Interview Question: Difference between a struct and a class in C#


Welcome to the Interview Questions series on C#. Last week we discussed about “ref” and “out” keywords of C# with suitable example and I hope, that gave you clear understandings about those two parameter type.

 

This week we will discuss about struct and class as few people in Facebook asked me to clarify those two types.

 

In .NET, there are two categories of types available named reference type and value type. The reference typed variables resides in heap and uses pointers to some memory locations whereas the value typed variables resides in the stack as value.

 

Class is reference type and struct is value type. That means, class as a reference type variable contains a pointer or a reference to some memory location where the actual value resides whereas struct as a value type variable contains the entire value.

 

Here is a comparison of class and struct which you can refer:

 

Class Struct
A class is reference type A struct is a value type
Class points to a memory location where the object is created Struct contains the entire struct value
As class is reference type, copying the object of a class into another variable copies only the reference. That means, you have multiple reference of the same object. As struct is value type, copying the struct variable to another struct variable copies the entire struct value.
Resides in the heap as memory location pointing to the actual value present in stack Resides directly in stack
Supports inheritance Does not support inheritance

 

 

Let’s see and example below which will clarify maximum of your doubts. In the below code snippet, we declared a class named “Person”. Now, we created the first object of Person type, set it in a variable after modifying it’s property called “Name”. We created another Person variable and assigned the first variable to it.

 

As the class is of reference type, “person1” and “person2” will point to the same memory location where the actual value will reside. So, in this stage if we change the property of “person2” it will effect “person1” too.

 
public class Person
{
    public string Name { get; set; }
}
 
class Program
{
    static void Main(string[] args)
    {
        Person person1 = new Person();
        person1.Name = "Kunal Chowdhury";
        Console.WriteLine("Person 1 object contains: " + person1.Name);
        Console.WriteLine("===========================================\n\r");
        
        Person person2 = person1;
        person2.Name = "Abhijit Jana";
        Console.WriteLine("Person 1 object contains: " + person1.Name);
        Console.WriteLine("Person 2 object contains: " + person2.Name);
    }
}

 

Thus changing the value of “person2” will also change the value of “person1” and after the new assignment, both will output same value as shown below:

 

 

Let us create a structure now named “Customer”. As we did for the class, we will create an instance of Customer here first and assign a value to it’s property called “Name”. Now we will assign “customer1” to another variable named “customer2” of type “Customer” and modify the “Name” property.

 

As “Customer” is value type, it will copy the entire value of “customer1” to “customer2”. Thus modifying the second instance will not change the first instance.

 

 
public struct Customer
{
    public string Name { get; set; }
}
 
class Program
{
    static void Main(string[] args)
    {
        Customer customer1 = new Customer();
        customer1.Name = "Kunal Chowdhury";
        Console.WriteLine("Customer 1 structure contains: " + customer1.Name);
        Console.WriteLine("================================================\n\r");
        
        Customer customer2 = customer1;
        customer2.Name = "Abhijit Jana";
        Console.WriteLine("Customer 1 structure contains: " + customer1.Name);
        Console.WriteLine("Customer 2 structure contains: " + customer2.Name);
    }
}

 

In this case, we will see the assigned value of customer1 after modifying the value of customer2 as shown below:

 

 

I hope that, this was clear for you to understand the basics of class, struct and will not again confuse you later in any interview. These are some very basic stuffs asked in every interview. Also, this will help you to decide the best type while working on a module of an application to optimize the memory properly.

 

Still have doubt? Or, want to share more about those? Leave a line below in the comment section to have a proper discussion with other experts.


4 comments

  1. Whenever I read "value type" and "stack" in the same sentence I have to recommend to read that: http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

    ReplyDelete
  2. Value type instances goes to the stack is wrong : http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx

    ReplyDelete
  3. One of the common questions I ask is 'What are the similarities between class and struct?'.

    ReplyDelete
  4. You forget about default ctor in struct....

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top