vote buttons
0
1
beginner
0
intermediate
0
advanced
15-Nov-2014 07:06 UTC
K Bonneau
350

1 Answers

vote buttons
0

Class Struct
Is a Reference Type Is a Value Type
Can inherit from other classes Cannot inherit from other classes or value types, but are allowed to implement interfaces
You can define a default (parameterless) constructor in a class Default constructors cannot be defined for Structs.
While instantiating, you must use the "new" keyword "new" keyword can be omitted while instantiating a struct.
Classes can have fields with initial value. E.g private int value = 0; Struct instance fields cannot be initialized, and always get initialized with the default value for the type of the field.
By default classes are not sealed but can be made sealed by using the sealed keyword. Structs are always sealed.
Can be null Is never null
Can have destructors Structs are not permitted to have destructors
Assignment to a variable of a class type references the same object being assigned Assignment to a variable of a struct type creates a copy of the value being assigned


MSDN recommends using structs if:

  • It logically represents a single value, similar to primitive types (intdouble, etc.).

  • It has an instance size under 16 bytes.

  • It is immutable.

  • It will not have to be boxed frequently.


15-Nov-2014 07:10 UTC
Rahul
100