Difference Between Class And Structure in C#

Class (Reference Type) v/s Structure (Value Type)

This is the very common C# interview question. Sometimes we took it very casually that we know everything about class and structure. Because these things we are reading out from our school days. But the reality is totally different. There are lots of untouched things about classes and structures.

Let’s start and test your skills with Class v/s Structure in C#. Below are the bullet points and after that, we will have a look through the programming examples.

Classes Only:

  • A class is a reference type so it is saved into Heap Memory.
  • Support for inheritance and polymorphism.
  • The reference can be null.
  • Having memory overhead per new instance.
  • All the members of a class are Private by default.
  • Class objects are deallocated or destroyed by the garbage collector when the instance is no longer referenced by any other code.
  • Sizeof empty class is 1 Byte.
  • a class object cannot be created without using the new keyword.
    Student obj=new Student ();

Structs Only:

  • A struct is a Value Type (passed by value (like integers)) so it is saved into Stack Memory.
  • Cannot support inheritance and polymorphism. So Structs are sealed type.
  • Cannot have a null reference (unless Nullable is used)
  • Do not have a memory overhead per new instance – unless ‘boxed’
  • Cannot have an explicit parameterless constructor.
  • Can not have Destructor.
  • All the members of a structure are Public by default.
  • Value types destroyed immediately after the scope is lost. Structures can not use the garbage collector for memory management.
  • Sizeof empty structure is 0 Byte.
  • The structure does not require new keyword. While new can be used on a struct to call a constructor.
    Student 
    obj;      OR         Student s = new Student (25);

Similarities between class and structure:

  • In C# language, classes and structures both can have Class Constructor, Private field, Methods, Properties, Indexer, and Event.
  • Both can support interfaces.
  • Operators overloading can be supported by both.
  • Both Can be generic.
  • We can define both as partial.

Let’s Guess the output of the following program.

using System;

namespace PlayingWithStructure
{
    struct Employee
    {
        public int EmpID { get; private set; }

        public Employee(int id)
        {
            EmpID = id;
        }

        public int IncrementEmpID()
        {
            EmpID++;
            return EmpID;
        }
    }

    class Program
    {
        public Employee PropEmployee { get; } = new Employee(10);
        public readonly Employee ReadonlyEmployee = new Employee(10);
        public Employee FieldMemberEmployee = new Employee(10);

        public void Test()
        {
            Console.WriteLine("===========Property case=============");
            Console.WriteLine(PropEmployee.IncrementEmpID());
            Console.WriteLine(PropEmployee.IncrementEmpID());

            Console.WriteLine("===========Readonly case=============");
            Console.WriteLine(ReadonlyEmployee.IncrementEmpID());
            Console.WriteLine(ReadonlyEmployee.IncrementEmpID());

            Console.WriteLine("===========Field Member case==========");
            Console.WriteLine(FieldMemberEmployee.IncrementEmpID());
            Console.WriteLine(FieldMemberEmployee.IncrementEmpID());
        }
        static void Main(string[] args)
        {

            new Program().Test();
        }
    }
}

OutPut:

Output of Structure
Output of Structure

 

You can download this sample by clicking here: Click Here

 

Written by 

Leave a Reply

Your email address will not be published. Required fields are marked *