Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ASP.NET : Class vs Structure in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 227
    Comment on it

     Class vs Structure in C#

    1) Class variables and structure variables are stored in different memory location. Class variables are stored on heap whereas structure variable are stored on stack.

     

    Explanation of how class variable and structure variable are actually stored in memory location:

     

    For class : Whenever a reference of a class is created that reference is stored in memory called stack pointing to an address in memory called heap which is storing actual data(variables).

     

    using System;
    
    namespace Test
    {
        class Demo
        {
            public int a = 10;
            public static void Main(string[] arg)
            {
                Demo obj = new Demo();
                Demo obj1 = obj;                 //When a reference is assigned to another reference,both point to same address
                System.Console.WriteLine("Value of a pointed by reference obj:{0}", obj.a);
                System.Console.WriteLine("Value of a pointed by reference obj1:{0}", obj1.a);
                obj1.a = 20;   //changing value of a pointed by reference obj1 also changes value of a pointed by reference obj because they are pointing to same address.
                System.Console.WriteLine("Value of a in obj after changing value in obj1:{0}", obj.a);
                System.Console.WriteLine("Changed vale of a in obj1:{0}", obj1.a);
                Console.ReadKey();
            }
        }
       
    }

    Output:

    Value of a pointed by reference obj:10
    Value of a pointed by reference obj1:10
    Value of a in obj after changing value in obj1:20
    Changed vale of a in obj1:20
    

    For structures : Stack is the memory where structure variable are stored.

    using System;
    
    namespace Test
    {
        struct Demo
        {
            public int a;
    
            public static void Main(string[] arg)
            {
                Demo obj = new Demo();
                obj.a = 10;
                //in structure when one reference obj is assigned to another obj1,compiler creates a copy of obj object and assigned that memory location to obj1. 
                Demo obj1 = obj; 
                System.Console.WriteLine("Value of a in obj:{0}", obj.a);
                System.Console.WriteLine("Value of a in obj1:{0}", obj1.a);
                obj1.a = 20;  //changing value of a in reference obj1 does not changes value of a in reference obj.
                System.Console.WriteLine("Value of a in obj after changing value in obj1:{0}", obj.a);
                System.Console.WriteLine("Changed value of a in obj1:{0}", obj1.a);
                Console.ReadKey();
            }
        }
    }

    Output:

    Value of a in obj:10
    Value of a in obj1:10
    Value of a in obj after changing value in obj1:10
    Changed value of a in obj1:20
    

    2) The member variables in structure are public by default but in class variables are private by default.

    3) When using class initialization of member variables is possible whereas structure do not allow initialization of member variables.

    4) Keywords such as sealed,static and abstract can be used with classes but structures do not make use of these keywords.

    5) Classes support inheritance and polymorphism but structure do not.

    6)  A class can have destructor but structure do not.

    7) A class calls a static constructor as soon as an object of class is created but static constructor is not triggered in case of structures.

    8) A structure contains a built-in public default constructor and does not allow explicit definition of parameterless constructor whereas class allow explicit definition of parameterless constructor.

    9) Keyword “sizeof ” can be used with structures but not with classes.

    10) Both class and structure can be empty but a null cannot be assigned to struct variable whereas a class reference can point to null.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: