What's the difference between struct and class in .NET?

前端 未结 19 1391
一向
一向 2020-11-22 01:51

What\'s the difference between struct and class in .NET?

19条回答
  •  温柔的废话
    2020-11-22 02:04

    Besides the basic difference of access specifier, and few mentioned above I would like to add some of the major differences including few of the mentioned above with a code sample with output, which will give a more clear idea of the reference and value

    Structs:

    • Are value types and do not require heap allocation.
    • Memory allocation is different and is stored in stack
    • Useful for small data structures
    • Affect performance, when we pass value to method, we pass the entire data structure and all is passed to the stack.
    • Constructor simply returns the struct value itself (typically in a temporary location on the stack), and this value is then copied as necessary
    • The variables each have their own copy of the data, and it is not possible for operations on one to affect the other.
    • Do not support user-specified inheritance, and they implicitly inherit from type object

    Class:

    • Reference Type value
    • Stored in Heap
    • Store a reference to a dynamically allocated object
    • Constructors are invoked with the new operator, but that does not allocate memory on the heap
    • Multiple variables may have a reference to the same object
    • It is possible for operations on one variable to affect the object referenced by the other variable

    Code Sample

        static void Main(string[] args)
        {
            //Struct
            myStruct objStruct = new myStruct();
            objStruct.x = 10;
            Console.WriteLine("Initial value of Struct Object is: " + objStruct.x);
            Console.WriteLine();
            methodStruct(objStruct);
            Console.WriteLine();
            Console.WriteLine("After Method call value of Struct Object is: " + objStruct.x);
            Console.WriteLine();
    
            //Class
            myClass objClass = new myClass(10);
            Console.WriteLine("Initial value of Class Object is: " + objClass.x);
            Console.WriteLine();
            methodClass(objClass);
            Console.WriteLine();
            Console.WriteLine("After Method call value of Class Object is: " + objClass.x);
            Console.Read();
        }
        static void methodStruct(myStruct newStruct)
        {
            newStruct.x = 20;
            Console.WriteLine("Inside Struct Method");
            Console.WriteLine("Inside Method value of Struct Object is: " + newStruct.x);
        }
        static void methodClass(myClass newClass)
        {
            newClass.x = 20;
            Console.WriteLine("Inside Class Method");
            Console.WriteLine("Inside Method value of Class Object is: " + newClass.x);
        }
        public struct myStruct
        {
            public int x;
            public myStruct(int xCons)
            {
                this.x = xCons;
            }
        }
        public class myClass
        {
            public int x;
            public myClass(int xCons)
            {
                this.x = xCons;
            }
        }
    

    Output

    Initial value of Struct Object is: 10

    Inside Struct Method Inside Method value of Struct Object is: 20

    After Method call value of Struct Object is: 10

    Initial value of Class Object is: 10

    Inside Class Method Inside Method value of Class Object is: 20

    After Method call value of Class Object is: 20

    Here you can clearly see the difference between call by value and call by reference.

提交回复
热议问题