Difference between Structs (Structures) vs Classes in C#

Total Post:46


C#  ADO.Net  OOPS 
 494  View(s)
Rate this:

Please describe in brief...

  1. Re: Difference between Structs (Structures) vs Classes in C#

    Difference between Structs (Structures) vs Classes in C#

    1. Structs are value types and classes are reference types.
    2. The value type is known as Stack and Reference Type is known as Heap.
    3. The struts are lightweight versions of classes. Structs are value types and can be used to create objects that behave like built-in types.
    4. The structs cannot have a default constructor (a constructor without parameters) or a destructor.
    5. The Structs are a value type and copied on assignment.
    6. The Structs are value types while classes are reference types.
    7. The Structs can be instantiated without using a new operator.
    8. The structs cannot inherit from another structs or class, and it cannot be the base of a class. Every struct inherits directly from System.ValueType, which inherits from System.Object.
    9. A Structs cannot be base class. So, Structs types cannot abstract and are always implicitly sealed.
    10. The Abstract and sealed modifiers are not allowed and structs member cannot be protected or protected internals.
    11. The Function members in structs cannot be abstract or virtual, and the override modifier is allowed only to the override methods inherited from system.ValueType.
    12. A Structs does not allow the instance field declarations to include variable initializers. However, static fields of structs are allowed to include variable initializers.
    13. The structs can implement interfaces.
    14. The structs can be used as a nullable type and can be assigned a null value.

    when to use struct or classes?

    Classes Structs
    The Assignments of large reference types are cheaper than assignments of large value types. The Allocations and de-allocations of value types are in general cheaper than allocations and de-allocations of reference types.
    Into the classes, two variables can contain the reference of the same object and any operation on one variable can affect another variable.

    Into the structs, each variable contains its own copy of the data (except in the case of the ref and out parameter variables), and any operation on one variable does not affect another variable. 

    The Structs are value types, allocated either on the stack or inline in containing types. The Classes are reference types, allocated on the heap and garbage-collected. 

    In this way, struct should be used only when you are sure that -

    This logically represents a single value, like primitive types (int, double, etc.).

    This is immutable.

    This should not be boxed and un-boxed frequently.

    Into the all other cases, you should define your types as classes.

    struct Location 
        public int a, b;
        public Location(int a, int b)
            this.a= a;
            this.b= b;
    Locationa = new Location(20, 20);
    Location z = y;
    y.a = 100;
    System.Console.WriteLine(z.a) ;  

    Thank You!!! for reading this article.



    I hope this will definitely help you.


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.