Some important questions that people ask in forums is "what's the difference between a structure and a class?" and "what could be the value of a structure since we already have classes that can do the job perfectly?". Those questions could particularly be posed by a Java programmer that wants to migrate from Java to .Net. For this reason I will list in this article some of those differences to make the issue clear.
-
Structures are value types and the classes are reference types. Before proceeding to the next point, let explain the difference between the two types. Imagine this is the memory within the machine,
Figure 1
The value types are stored in the stack but the reference types are not. In fact, what could be really stored in the stack for the reference types is a pointer that targets an address at the heap level.
Then the type of structure objects are stored in the stack exactly like any value type, say an integer, a double or a float. Meanwhile, memory locations could be reserved for reference types in the heap. Defining the heap and stack and the difference between them is beyond the scope of this article but nevertheless I propose this excellent Matthew article to understand the memory mechanisms.
C# Heap(ing) Vs Stack(ing) In .NET - Part One
-
Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.
-
Classes can be inherited whereas structures not.
-
A structure couldn't be null like a class.
-
A structure couldn't have a destructor such as a class.
-
A structure can't be abstract, a class can.
-
You cannot override any methods within a structure except the following belonging to the type object:
-
Equals()
-
GetHashCode()
-
GetType()
-
ToString()
And the other polymorphism technique used for structures is implementing interfaces.
-
Declared events within a class are automatically locked and then they are thread safe, in contrast to the structure type where events can't be locked.
-
A structure must always have the default parameter less constructor defined as public but a class might have one, so you can't define a private parameter-less constructor as in the following,
- struct Me
- {
- private Me()
- {
- }
- }
-
- class Me
- {
- private Me()
- }
-
A static constructor is triggered in the case of a class but not in the case of a structure as in the following,
- struct myStructure
- {
- static myStructure()
- {
- Console.WriteLine("This is me a structure");
- }
- }
- class myClass
- {
- static myClass()
- {
- Console.WriteLine("This is me a class");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- myStructure s =new myStructure();
- myClass c =new myClass();
- Console.Read();
- }
- }
-
The structure can't contain a volatile field whereas the class can
-
You can't use sizeof with classes but you can with structures
-
Fields are automatically initialized with classes to 0/false/null whether as structures are not
-
Fields can't be directly instantiated within structures but classes allow such operations as in the following,
- struct myStructure
- {
- public string x = 2;
- }
- class myClass
- {
- public string x = 2;
- }
-
Structures and classes don't adopt the same approach for the System.Object.Equals() method.
Assume the following structure and class,
- struct StructurePerson
- {
- public string FirstName;
- public string LastName;
- }
- class Person
- {
- public string FirstName;
- public string LastName;
- }
Now, try this code,
- class Program
- {
- static void Main(string[] args)
- {
- StructurePerson strX =new StructurePerson();
- strX.LastName = "Bejaoui";
- strX.FirstName = "Bechir";
- StructurePerson strY =new StructurePerson();
- strY.LastName = "Bejaoui";
- strY.FirstName = "Bechir";
-
- if (strX.Equals(strY))
- {
- Console.WriteLine("strX = strY");
- }
- else
- {
- Console.WriteLine("strX != strY");
- }
- ClassPerson clsX =new ClassPerson();
- clsX.LastName = "Bejaoui";
- clsX.FirstName = "Bechir";
- ClassPerson clsY =new ClassPerson();
- clsY.LastName = "Bejaoui";
- clsY.FirstName = "Bechir";
- if (clsX.Equals(clsY))
- {
- Console.WriteLine("clsX = clsY");
- }
- else
- {
- Console.WriteLine("clsX != clsY");
- }
- Console.Read();
- }
- }
In the first structure the two objects are value types, they are compared depending on their values like int I = 5 and int J = 5 so I=J because they have the same value. In the contrast, in the class case of two different and distinct references, to make clsX = clsY you should use the following code,
- ClassPerson clsX =new ClassPerson();
- clsX.LastName = "Bejaoui";
- clsX.FirstName = "Bechir";
- ClassPerson clsY = clsX;
- if (clsX.Equals(clsY))
- {
- Console.WriteLine("clsX = clsY");
- }
- else
- {
- Console.WriteLine("clsX != clsY");
- }
That's it.
Good Dotneting!!!