What is difference between class and structure in C Sharp? Also, tell similarities between them.

Answer of this technical C# interview includes difference between class and structure in c# programming and similarities between them.
Answer

Similarities between class and structure in C#

  • In C# language, classes and structures both can have Class Constructor, Private field, Methods , Properties and Event.
  • Both can support interfaces.

Difference between class and structure in C#

  • As per memory point of view, Structure is stored on stack memory and Class is stored on heap memory. Though, for both, class and structure, objects are constructed using “new” keyword.
  • Important difference is that structure cannot have a destructor but a class can have.
  • A structure cannot have explicit constructor without parameter.
  • Structure is a value type whereas Class is a reference type.
  • Structure does not support inheritance in c# languge.
  • When we pass structure object to a function, a copy of object is passed. And, received object will have scope within function only. Hence, if we modify structure object inside a function it will not change original one resides within the calling routine.
    But, when a class object is passed to a function, then object reference is passed. Means, address of the object will be passed. So, if we modify that object within function, it will also affect original object. Supportive example below.

Below C# code example shows that if structure object is passed to a function and get modified within it, it will not change the original value. But, in the case of class object, it will change the original value.

Output of below program:
After class object Creation
Number is:10
After Modification of CClass object
Number is:30
After Structure object Creation
Number is:10
After Modification of SStruct object
Number is:10

Note that original value of struct object is not changed after ModifyStructObj() call.But, for class it has changed the original value. i.e. from 10 to 30.

class CClass
{
    public int num;

    public CClass(int num)
    {
        this.num = num;
    }
    public void display()
    {
        Console.WriteLine("Number is:"+ num);
    }

}
struct SStruct
{
    public int num;
    public SStruct(int num)
    {
        this.num = num;
    }
    public void display()
    {
        Console.WriteLine("Number is:" + num);
    }
}

class Program
{
    static void Main(string[] args)
    {        
        //class
        CClass c = new CClass(10);
        Console.WriteLine("After class object Creation");
        c.display();
        ModifyClassObj(c);
        Console.WriteLine("After Modification of CClass object");
        c.display();

        // structure

        SStruct s = new SStruct(10);
        Console.WriteLine("After Structure object Creation");
        s.display();
        ModifyStructObj(s);
        Console.WriteLine("After Modification of SStruct object");
        s.display();              
       
    }

    private static void ModifyClassObj(CClass classObj)
    {
        classObj.num += 20;
    }
    private static void ModifyStructObj(SStruct structObj)
    {
        structObj.num += 20;
    }
}

Related Posts