Some Real Differences Between Structures and Classes

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.

  1. 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,


    structure

    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.

    http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx?ArticleID=9adb0e3c-b3f6-40b5-98b5-413b6d348b91
     
  2. Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.
  3. Classes can be inherited whereas structures not.
  4. A structure couldn't be null like a class.
  5. A structure couldn't have a destructor such as a class.
  6. A structure can't be abstract, a class can.
  7. 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.
     
  8. 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.
  9. 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,
    1. struct Me  
    2.     {  
    3.         private Me()// compile-time error  
    4.         {  
    5.         }  
    6.     }  
    7.        
    8. class Me  
    9.          {  
    10.             private Me()// runs Ok{  
    11.          }   
  10. A static constructor is triggered in the case of a class but not in the case of a structure as in the following,
    1. struct myStructure  
    2.     {  
    3.         static myStructure()   
    4.         {  
    5.             Console.WriteLine("This is me a structure");  
    6.         }  
    7.     }  
    8.     class myClass  
    9.     {  
    10.         static myClass()  
    11.         {  
    12.             Console.WriteLine("This is me a class");  
    13.         }  
    14.     }  
    15.     class Program  
    16.     {  
    17.         static void Main(string[] args)  
    18.         {  
    19.            myStructure s =new myStructure();//Nothing happen  
    20.            myClass c =new myClass();//Will out put This is me a class  
    21.            Console.Read();  
    22.         }  
    23.     }  
  11. The strucutre can't conatain a volatile field whereas the class can
  12. You can't use sizeof with classes but you can with structures
  13. Fields are automatically initialized with classes to 0/false/null wheatheras strucutres are not
  14. Fields can't be directley instantiated within structures but classes allow such operations as in the following,
    1. struct myStructure  
    2.     {  
    3.         publicstring x = 2;//Not allowed   
    4.     }  
    5.     class myClass  
    6.     {  
    7.         publicstring x = 2;//Allowed  
    8.     }  
  15. Structures and classes don't adopt the same aproach for the System.Object.Equals() method.

    Assume the following strucutre and class,
    1. struct StructurePerson  
    2.     {  
    3.         publicstring FirstName;  
    4.         publicstring LastName;  
    5.     }  
    6.     class ClassPerson  
    7.     {  
    8.         publicstring FirstName;  
    9.         publicstring LastName;  
    10.     }  
    Now, try this code,
    1. class Program  
    2.     {  
    3.         static void Main(string[] args)  
    4.         {  
    5.             StructurePerson strX =new StructurePerson();  
    6.             strX.LastName = "Bejaoui";  
    7.             strX.FirstName = "Bechir";  
    8.             StructurePerson strY =new StructurePerson();  
    9.             strY.LastName = "Bejaoui";  
    10.             strY.FirstName = "Bechir";  
    11.   
    12.             if (strX.Equals(strY))  
    13.             {  
    14.                 Console.WriteLine("strX = strY");  
    15.             }  
    16.             else  
    17.             {  
    18.                 Console.WriteLine("strX != strY");  
    19.             }//This code displays strX = strY  
    20.             ClassPerson clsX =new ClassPerson();  
    21.             clsX.LastName = "Bejaoui";  
    22.             clsX.FirstName = "Bechir";  
    23.             ClassPerson clsY =new ClassPerson();  
    24.             clsY.LastName = "Bejaoui";  
    25.             clsY.FirstName = "Bechir";  
    26.             if (clsX.Equals(clsY))  
    27.             {  
    28.                 Console.WriteLine("clsX = clsY");  
    29.             }  
    30.             else  
    31.             {  
    32.                 Console.WriteLine("clsX != clsY");  
    33.             }//This code displays clsX != clsY  
    34.             Console.Read();  
    35.         }  
    36.     }  
    In the first strucutre 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,

     

    1. ClassPerson clsX =new ClassPerson();  
    2. clsX.LastName = "Bejaoui";  
    3. clsX.FirstName = "Bechir";  
    4. ClassPerson clsY = clsX;  
    5. if (clsX.Equals(clsY))  
    6. {  
    7.     Console.WriteLine("clsX = clsY");  
    8. }  
    9. else  
    10. {  
    11.     Console.WriteLine("clsX != clsY");  
    12. }//This code displays clsX = clsY  

That's it.

Good Dotneting!!!

Next Recommended Readings