David Klempfner
David Klempfner

Reputation: 9870

Is a struct just a class that derives from ValueType?

Is it true to say that a struct is just a class which inherits from System.ValueType?

Is the keyword "struct" just syntactic sugar for writing a class with : System.ValueType after the name?

If it is just a class, then is it true to say that not all classes are reference types since structs are technically classes?

Upvotes: 3

Views: 1037

Answers (2)

James Michael Hare
James Michael Hare

Reputation: 38397

Not quite "just syntactical sugar". From the MSDN:

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and Structure…End Structure in Visual Basic) to support the creation of value types.

So could you say that a struct is just a class that inherits from System.ValueType semantically? That'd debatable. All struct are derived from System.ValueType, but you can't explicitly create a class that derives from System.ValueType.

In addition, of course, just being derived from System.ValueType, struct has a lot of differences from class as you probably know. But if not, I have a blog post on some of the key differences here, including but not limited to:

  • Value types are, of course, passed and assigned by value, not by reference.
  • A struct cannot accept initialization values for their fields in the definition (they always given values for their declared field types).
  • A struct can have events, but since they are value types must take care that you aren't subscribing to a copy!
  • You cannot inherit from a struct.
  • You cannot create struct parameterless constructor, struct provides one which cannot be overridden.
  • Creating an overloaded struct constructor does not hide the parameterless constructor.
  • The this keyword, used in a struct is a value variable, not a reference.
  • You do not need to use new to create an instance of a struct (but if you do this you must provide a value for all fields before it is used.

MSDN also has some good advice on when to use struct vs class. Because they are value types, you should think of them as such and limit them to smaller things (16 bytes or less) and preferably as immutable representations of a single value (like DateTime, TimeStamp etc.).

Upvotes: 11

Jeremy Thompson
Jeremy Thompson

Reputation: 65534

Class and struct differences

Structs differ from classes in several important ways:

  • Structs are value types (Section 11.3.1).
  • All struct types implicitly inherit from the class System.ValueType (Section 11.3.2).
  • Assignment to a variable of a struct type creates a copy of the value being assigned (Section 11.3.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 (Section 11.3.4).
  • Boxing and unboxing operations are used to convert between a struct type and object (Section 11.3.5).
  • The meaning of this is different for structs (Section 11.3.6).
  • Instance field declarations for a struct are not permitted to include variable initializers (Section 11.3.7).
  • A struct is not permitted to declare a parameterless instance constructor (Section 11.3.8).
  • A struct is not permitted to declare a destructor (Section 11.3.9).

See also: Choosing Between Classes and Structures

Upvotes: 5

Related Questions