What is the difference between a struct and a class in C#?
By in C# on Jul 13 2006
  • Rama
    Jul, 2014 29

    Structures are value types means the data they contain are stored on a stack on memory. Classes are reference types means the data they contain are stored as a heap on memory.Structures are implicitly derived from a class called System.Value type Classes are derived from System.ObjectWe can make a constructor for a structure but "no argument constructor is not possible". The structure's constructor always have a parameter. In classes no argument constructors are possible.Structures cannot have an instance field. Classes can have a instance field.Structure cannot inherit from other structure. Class can inherit from other class.

    • 0
  • Jul, 2006 13

    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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS