c# - Difference between class and structure.

Last updated:9/17/2020 11:11:07 AM

1 Answers

Anonymous User
Anonymous User

1. Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.

2. Classes can be inherited whereas structures not.

3. A structure couldn't have a destructor such as a class.

4. A structure can't be abstract, a class can.

5. A structure must always have the default parameter less constructor defined as public but a class might have one, so you can't define a private parameter-less constructor as in the following:

6. Class is a reference type and its object is created on the heap memory, whereas structure is a value type that is why its object is created on the stack memory.

7. Class can inherit another class, whereas structure does not support the inheritance.

8. Class object cannot be created without using the new keyword; it means we have to use it.  Demo obj=new Demo();

    whereas structure object can be created without using the new keyword.(optional) Demo obj;