[C#] Class vs Struct

C#’s struct is much more like a class. In C# or other OO languages, a struct can do everything that a class can do, especially from the developer’s perspective. So what’s the point? Do we even need a struct? Why not just use a class all the time.

Generally speaking, you are right. You do not need to use a struct at all in most cases. But in some cases, you might consider using a struct rather than a class. I will explain when you can use a struct type at the end of this article.

Let’s see how struct is different from a class and when a struct can be used instead of a class.

 

1. Struct acts like a class

  • A struct can have fields, properties, and methods
  • A struct can define constructors
  • A struct can implement interfaces

 

2. Yet, struct is not a class

  • A struct is a value type. (A class is a reference type)
  • A struct cannot provide a default (parameter-less) constructor. You can still define parameterized constructors to change the initial values
  • A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from “System.ValueType“. (Structures are implicitly sealed.)
  • When a struct is passed as an argument to a method it behaves like a value type and a copy is created on the stack, which involves more overhead than passing a reference of a class object
  • Unlike classes, structs can be instantiated without using a new operator. (You can use a new operator too)

 

3. Struct is a value Type

The most important thing you need to remember is that a struct is a value type.

Look at the following sample code:

struct MyVal
{
  public int X;
}
static void AddOne(MyVal v)
{
  v.X += 1;
}
static void Main(string[] args)
{
  MyVal val;
  val.X = 0;

  AddOne(val);
  Console.WriteLine(val.X);
  AddOne(val);
  Console.WriteLine(val.X);
  AddOne(val);
  Console.WriteLine(val.X);
}

So, can you guess the output?

The result is ,surprisingly, 0 0 0 . Whenever a structure is passed to a method, a new instance is created. So the original structure remains without changes. If you make “MyVal” as a class, the output will be 1 2 3.

 

4. How to create a Struct type

There are 2 ways to create a “struct” variables.

Firstly, you can simply create a variable and assign each piece of public fields before invoking its members. If you do not assign all public fields before making use of it, you will get a compiler error.

struct MyVal
{
  public int X;
}

static void Main(string[] args)
{
  MyVal val;
  val.X = 0; // if you comment this line, you will get a compiler error

  Console.WriteLine(val.X);
}

Secondly, you can use a “new” keyword just like a class. In this case, the constructor will be called and all fields are set to default values.

static void Main(string[] args)
{
  MyVal val = new MyVal(); // no error

  Console.WriteLine(val.X);
}

 

5. When to use a Struct – Struct Usage Guide lines

In most cases, it is recommended to use a class rather than a struct. But you can use a struct for specific cases.

  • It logically acts like a primitive type
  • It has an instance size under 16 bytes (4 integer variables)
  • It will not have to be boxed frequently

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s