What is the difference between a struct and a class in C#?

From language spec:

The list of similarities between classes and structs is as follows.

Longstructs can implement interfaces and can have the same kinds of members as classes. Structs differ from classes in several important ways; however, structs are value types rather than reference types, and inheritance is not supported for structs. Struct values are stored on the stack or in-line. Careful programmers can sometimes enhance performance through judicious use of structs. For example, the use of a struct rather

than a class for a Point can make a large difference in the number of memory allocations performed at runtime. The program below creates and initializes an array of 100 points. With Point implemented as a class, 101 separate objects are instantiated-one for the array and one each for the 100 elements.

Showing Answers 1 - 3 of 3 Answers

samiksc

  • Dec 7th, 2005
 

Following are the main points of difference between classes and structs in C#:

  1. Structs are value type and classes are reference type.
    • Whenever a struct object is assigned to another struct object a copy is created. In case of classes the same operation results in two references referring to same object: For example, a.x = 10; b = a; a.x = 20; cout << b.x will display '10' if a and b are struct objects and it will display '20' if a and b are class objects.
    • Boxing of a struct object creates a copy of the object in a different type, whereas, boxing a class object creates a reference (of different type) to the same object.
  2. Inheritance is

  Was this answer useful?  Yes

samiksc

  • Dec 7th, 2005
 

Following are the main points of difference between classes and structs in C#:

  1. Value type vs Reference type: Structs are value type and classes are reference type.
    • Whenever a struct object is assigned to another struct object a copy is created. In case of classes the same operation results in two references referring to same object: For example, a.x = 10; b = a; a.x = 20; cout << b.x will display '10' if a and b are struct objects and it will display '20' if a and b are class objects.
    • Boxing of a struct object creates a copy of the object in a different type, whereas, boxing a class object creates a reference (of different type) to the same object.
    • Since structs are value type it is not possible to assign null to a struct object.
    • When a struct object is passed to a function as a parameter it is passed by value, unless specifically marked as 'ref' or 'out' parameter.
  2. Inheritance: Inheritance is not allowed for structs.
    • All struct types implicitly inherit from System.ValueType and they are implicitly sealed.
    • The keywords related to inheritance concepts are not allowed for structs. For example, abstract, protected, virtual, override etc.
  3. Initialization: Structs cannot have default constructor.
    • All struct member variables of value type are initialized to their default values and reference member variables are initialized to null.
    • Field initializers (initialization in the member variable declaration line like int a = 0; ) is also not allowed for structs.
  4. Destructor: Structs cannot declare a destructor.
  5. Meaning of 'this':
    • For a struct instance constructor (parameterized) 'this' is treated as an out parameter.
    • For other member functions it is treated as a 'ref' parameter.
    • So it is possible to assign to 'this' and change the current object.
    • In case of a class 'this' is considered as a value, so it is not possible to assign to 'this' from inside a class.
    • Since 'this' is a 'ref' or 'out' parameter, it must be definitely assigned at every point where it is used.

             Class                                                                      Struct

1.This is reference Type                               1.This is Value Type

2.It Supports Inheritence                             2.It Doesnt Supports Inheritence

3. It Have Default Constructor                      3.It Doesnt Have any Default Constructors 

4.Object can be created by using                 4.We can create object without using "new"
"new" Keyword           

5.It is more useful in collections                    5. It is more useful in Arrays

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions