1. Classes are reference types and structs are value types.
2. A class variable can be assigned null because classes are reference type. But we cannot assign null to a struct variable because structs are value type.
3. When you instantiate a class, it will be allocated on the heap. When you instantiate a struct, it gets created on the stack.
4. Classes support inheritance. But there is no inheritance for structs.
5. Classes are used for complex and large set data. structs are simple to use.
6. Structures and Enumerations are Value-Types. This means, the data that they contain is stored as a stack on the memory. Classes are Reference-Types, means they are stored as a heap on the memory.
7. Classes can have explicit parameter less constructors. But structs cannot have this.
8. Objects created from classes are terminated using Garbage collector. Structures are not destroyed using GC.
9. By default, all of the members of a class are private and, by default, all of the members of a structure are public.
10. There is no data hiding features comes with structures. Classes do, private, protected and public.
11. A structure can't be abstract, a class can.
12. A structure is contain only data member , but class contain data member and member function.
13. Struct cannot have default constructor but class have default constructor.
14. Structs can't have destructors, but classes can have destructors.
Note : a struct is a value type, a class is a reference type. Value types hold their value in memory where they are declared, but reference types hold a reference to an object in memory. If you copy a struct, C# creates a new copy of the object and assigns the copy of the object to a separate struct instance. However, if you copy a class, C# creates a new copy of the reference to the object and assigns the copy of the reference to the separate class instance.
No comments:
Post a Comment