Introduction:
So, what is the difference between a struct and a class? Well, if you have only ever been exposed to the Java world, there is no concept of a struct as all complex types are classes. But if you've only ever been exposed to the C++ world, you would probably answer that C++ structs and classes are identical in every way save one: structs are public by default and classes are private by default.
So, with C++ and Java both counted in the ancestry of C#, which route did the designers of C# take when they went to implement class and stuct? Well, it turns out that C# struck out on its own and took neither side. Struct is not simply a public-defaulted class, but they didn't eliminate it either.
C# is unique among the triumvirate in its differences between what it calls a struct and what it calls a class. While there are a lot of good articles and books that accurately describe the difference, there are also unfortunately a lot of generalizations and incomplete descriptions of the differences.
Repetition is the key to learning, and hopefully by this blog repeating the differences those who are new to C# will learn something, and those who are intermediate will refresh themselves, and those who are advanced can point out any holes I may have missed.
Semantics:
First of all, it is important to note that there is a strong semantic difference in people's mind when you say struct or class that needs to be considered. Typically when you tell someone you've defined a class, this tends to trigger in people's mind the idea that you've created a fully-fledged object-oriented type that follows all the basic OO rules (encapsulation, polymorphism, etc) and has its actual fields well hidden and protected.
However, when you say you've defined a struct, a lot of times people get the image of a very flat data structure with simple fields (or properties) and very few operations. That is, many people think fo structs as lightweight, flat types and classes as full-blown object oriented types.
So, regardless of what the language describes as the differences (which we will get to next section), just keep in mind that people tend to think of classes as object-oriented types and structs as flat "data-holder" types.
Syntactical Comparison:
Now, semantics aside, there are a lot of things that are similar between struct and class in C#, but there are also a fair number of surprising differences. Let’s look at a table that sums them up:
Value vs. Reference Type
This one is probably the one key difference most people will point to as the primary difference between struct and class in C#, and not surprisingly, it is the source of several potential pitfalls.
The fact that a struct is a value type, while class is a reference type. This has several ramifications:
With value types:
- Value type assignments copy all members the whole value - this copies all members of one value to another making two complete instances.
- Value types passed by parameter or returned from methods/properties copy whole value - this behavior is the same as value assignment.
- Value types assigned to an object are boxed – that is, they are surrounded by an object and then passed by reference.
- Value types are destroyed when they pass out of scope - local variables and parameters are typically cleaned up when scope is exited, members of an enclosing type are cleaned up when the enclosing type is cleaned up.
- Value types may be created on the stack or heap as appropriate - typically parameters and locals are created on the stack, members of an enclosing class are typically on the heap.
- Value types cannot be null - default value of members that are primitive is zero, default value of members that are a struct is an instance with all struct members defaulted.
In contrast, with reference types:
- Reference type assignment only copies the reference - this decrements/increments reference counts as appropriate.
- Reference types passed by parameter or returned from methods/properties pass a reference - the reference is copied, but both references refer to the same original object.
- Reference types are only destroyed when garbage collected - after all references to the object are determined to be unreachable.
- Reference types are generally on the heap - it’s always possible compiler may optimize, but in general you should always think of them as heap objects
- Reference types can be null - default value of members that are reference types members is null.
Blogger Labels: Difference,Struct,Value,Type,Class,Introduction,Java,concept,ancestry,designers,triumvirate,differences,generalizations,descriptions,Repetition,Semantics,data,properties,holder,Syntactical,Comparison,Reference,pitfalls,fact,ramifications,instances,parameter,assignment,scope,parameters,locals,zero,instance,increments,references,garbage
No comments:
Post a Comment