Structs in C#

This article introduces you to the differences between classes and structs in C#, and explains how to use structs correctly.

Why we need Structs?

The basic reason is the ability to create types with value semantics, which, if properly used, leads to better performance in a managed environment.

.NET supports the notion of value types and reference types. Instances of reference types get allocated in the managed heap and are garbage collected when there are no outstanding references to them. Instances of value types, on the other hand, are allocated in the stack, and hence allocated memory is reclaimed as soon as their scope ends. All C# primitive data types, except for System.String, are value types.

In C#, structs are value types, classes are reference types. There are two ways you can create value types, in C#, using the enum keyword and the struct keyword. Using a value type instead of a reference type will result in fewer objects on the managed heap, which results in lesser load on the garbage collector (GC), less frequent GC cycles, and consequently better performance. However, value types have their downsides too. Passing around a big struct is definitely costlier than passing a reference, that's one obvious problem. The other problem is the overhead associated with boxing/unboxing. Apart from performance, there are times when you simply need types to have value semantics, which would be very difficult to implement if reference types are all you have.

Differences between Class & Struct:

  1. Struct and Inheritance

structs derive from System.ValueType whereas classes derive from System.Object or one of its descendants. Of course, System.ValueType again derives from System.Object, but that's beside the point. structs cannot derive from any other class/struct, nor can they be derived from. However, a struct can implement any number of interfaces. Although, that when you treat the struct as an interface, it gets implicitly boxed, as interfaces operate only on reference types. E.g.

struct Fun : IFun

{

int x;

}

and then:

IFun iFun = new Fun();

an instance of Foo is created and boxed. All interface method calls then execute only on the boxed instance.

  1. Constructors:

Although the CLR allows it, C# does not allow structs to have a default parameterless constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor. So, even if you happened to define a default constructor, it will not be called and that will only confuse you. To avoid such problems, the C# compiler disallows definition of a default constructor by the user. And because it doesn't generate a default constructor, you can't initialize fields when defining them, like:

struct MyWrongFun

{

int x = 1;

}

The compiler puts all this initialization code into the constructor (every constructor), and because there's no default constructor, you can't do the initialization.

  1. Destructors:

You cannot define destructors for structs. The compiler straightaway flags it as an error. Of course, structs can implement IDisposable (it being an interface), so you can always use the dispose pattern (with the extra boxing overhead).

When to use Structs?

When not to use Structs?