Frequently Asked Interview Questions

Difference between struct and class in C#

What is the Difference between struct and class in C#?

Structs differ from classes in several important ways:
  1. Structs are value types
  2. Assignment to a variable of a struct type creates a copy of the value being assigned. This differs from assignment to a variable of a class type, which copies the reference but not the object identified by the reference
  3. The default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null
  4. Boxing and unboxing operations are used to convert between a struct type and object.
  5. Instance field declarations for a struct are not permitted to include variable initializers

  6.                    struct Point
                      {
                           public int x = 1; // Error, initializer not permitted
                           public int y = 1; // Error, initializer not permitted
                      }

  7. A struct is not permitted to declare a parameterless instance constructor
  8. A struct is not permitted to declare a destructor.

Most Visited Pages

Home | Site Index | Contact Us