views:

109

answers:

7

Possible Duplicate:
C# / .NET : when structures are better than classes?

Given the following type

class Person
{
    public int Id{get;}
    public Name Name{get;set;}
    public Address Address{get;set;}
}

public struct Name
{
    public string First{get;set;}
    public string Middle{get;set;}
    public string Last{get;set;}
}

public class Address
{
    public string Line1{get;set;}
    public string Line2{get;set;}
    public string City{get;set;}
    public string StateCode{get;set;}
    public string PostalCode{get;set;}

}

What will you use (class or struct) for the simple types Name and Address. What are the advantages of structs over classes in C#?

+4  A: 

Straight from the source (MSDN):

In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.

I couldn't have made it much more concise if I tried.

Justin Niessner
+5  A: 

In general: Always use classes unless you have a good reason to use a struct.

tster
+1 Also: even if you think you need a struct, you probably don't
Tim Robinson
Feels a bit like "Don't ask why.. Ask why not!"
Zyphrax
+3  A: 

It depends on what the mean in the application.

If they do not have an intrinsic ID attached, they would be a value type, hence I would use a struct.

If they have an identity of themselves, I would use a class.

Value types should be those that if all fields have the same value, then they are identical and you don't care which one you get (for instance, the number 3 - do you care which number 3 you are using?). They should also be immutable, that is never change once constructed.

If they don't conform to these guidelines, use classes to model them.

Oded
A: 

I found this page quite useful (sorry too large to quote):
http://www.jaggersoft.com/pubs/StructsVsClasses.htm

Zyphrax
+1  A: 

Use struct only if your type will be immutable, you are sure that you will never require inheritance and your type is small and simple. If this is true and your type has in your opinion "value semantics" you can think about using a struct.

But in all other cases alwys use classes, you will need to create structs very very rarely, in business applications nearly never.

codymanix
+1  A: 

Here are just some of the differences between a struct and a class:

  • Classes are reference types and structs are value types. A class variable can be assigned null, but we cannot assign null to a struct variable, since structs are value type.
  • Classes must be instantiated using the new operator, but structs can be instantiated without using the new operator.
  • Classes support inheritance. Structs do not.
  • All the fields of a struct must be fully initialized inside the constructor. In a class it's optional.
  • Structs object is allocated in the stack, and the class object is allocated in the heap.
  • Class are usually for large amounts of data, whereas structs are smaller, and often used when you want to group similar data together. Use a class when object identity is more important than value. Use a struct when the value contained by an instance is more important than instance identity.
  • When you pass a struct as a parameter, it can be more expensive than passing an instance of a reference type, due to the copying costs (remember, it's a value type, not a reference).

So for example, if Person was a struct you'd have to initialize the id, name and address in the constructor. You wouldn't have any inheritence and you can't extend it... So you wouldn't be able to do public class Employee : Person { ... }. Also, anytime you pass it into a function a copy of it is actually passed in. Now, on the flipside, maybe you want just that. Maybe you want it so that EVERY field has to be initialized, and it shouldn't extend anything or be used to extend another object. And, maybe you want to always work with copies of the object instead of the original.

myermian