I'm looking for a clear, concise and accurate answer.
Ideally as the actual answer, although links to good explanations welcome.
I'm looking for a clear, concise and accurate answer.
Ideally as the actual answer, although links to good explanations welcome.
Well, for starters, a struct is passed by value rather than by reference. Structs are good for relatively simple data structures, while classes have a lot more flexibility from an architectural point of view via polymorphism and inheritance.
Others can probably give you more detail than I, but I use structs when the structure that I am going for is simple.
I think this article "Type Fundamentals" by Jeffrey Richter is a very good place to start.
In .Net the struct and class declarations differentiate between reference types and value types.
When you pass round a reference type there is only one actually stored. All the code that accesses the instance is accessing the same one.
When you pass round a value type each one is a copy. All the code is working on it's own copy.
This can be shown with an example:
void ChangeInt( int input ) {
input = 25;
}
...
int testStruct = 15; //value type
ChangeInt( testStruct );
//value of testInt is still 15 - the method changed a copy
For a class this would be different
class MyClass {
string MyProperty { get; set; }
}
void ChangeMyClass ( MyClass input ) {
input.MyProperty = "new value";
}
...
MyClass testClass = new MyClass { MyProperty = "initial value" }; //ref type
ChangeMyClass ( testClass );
//value of testClass.MyProperty is now "new value"
// - the method changed the instance passed.
Classes can be nothing - the reference can point to a null.
Structs are the actual value - they can be empty but never null. For this reason structs always have a default constructor with no parameters - they need a 'starting value'.
Assuming it's similar to c++, a struct is a simple data structure that is used to contain several variables.
A class is a data structure that has defined operations (methods) and has protected variables for encapsulation.
Instances of classes are stored on the managed heap. All variables 'containing' an instance are simply a reference to the instance on the heap. Passing an object to a method results in a copy of the reference being passed, not the object itself.
Structures (technically, value types) are stored wherever they are used, much like a primitive type. The contents may be copied by the runtime at any time and without invoking a customised copy-constructor. Passing a value type to a method involves copying the entire value, again without invoking any customisable code.
The distinction is made better by the C++/CLI names: "ref class" is a class as described first, "value class" is a class as described second. The keywords "class" and "struct" as used by C# are simply something that must be learned.
Structs are the actual value - they can be empty but never null
This is true, however also note that as of .NET 2 structs support a Nullable version and C# supplies some syntactic sugar to make it easier to use.
int? value = null;
value = 1;
Yeah @dp, I thought that might be a little off topic, but it makes sense to mention that here.
You can also check with ??, so:
int? i = SomeFunctionThatMightGetAnInt();
//if i is null write 0, otherwise write i
Console.Write( i ?? 0 );
In .NET, there are two categories of types, reference types and value types.
Structs are value types and classes are reference types.
The general different is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.
A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.
A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.
This has one benefit, to begin with:
Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:
When you declare variables or fields, here's how the two types differ:
A short summary of each, with differences highlighted in italics:
Classes Only:
Structs Only:
Both Classes and Structs:
The question is pretty much answered at this point.
It might be of interest a quick and dirty guide to choosing between struct and class in every-day coding.
Remember the answer, as 99% of interviews I've had use it! Here's two more explanations to add the list:
Structure vs Class Structure is value type so stored in stack,but class is reference type stored in heap. Structure doesn't support inheritance,polymorphism but,class supports both. By default all the struct members are public but class members are by default private in nature. As structure is value type,we can't assign null to struct object,but it is not the case in class.