What Is Faster In C#: A Struct Or A Class?

Mark Farragher
4 min readMar 15, 2019

In this article I’ll take a look at structs and classes in C#.

Understanding the performance difference between these two will help you pick the correct code for every occasion.

Check out my code:

I’ve got a PointClass and a PointStruct, both containers for storing X and Y integer values. And there’s also a PointClassFinalized with a finalizer.

The MeasureTestA method allocates an array of 1,000,000 PointClassFinalized instances.

The MeasureTestB method is almost identical, but it allocates 1,000,000 instances of PointClass instead.

And there’s MeasureTestC which allocates 1,000,000 instances of PointStruct.

Which method do you think is fastest?

Let’s find out:

Did you expect that?

Let’s focus on MeasureTestB and MeasureTestC for now. The only difference between these two methods is that the one allocates classes, and the other allocates structs.

--

--