.NET Framework Custom Types

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


Typically a struct is used only when performance is very important. Since value types live on the stack, they can be accessed much quicker than classes. However, the stack has much less room than the heap, so structs should be kept small (Microsoft recommends structs take up no more than 16 bytes).

A class is the most-used type (of these three) in C#, and is generally what you should go with first.

An enum is used whenever you can have a clearly defined, distinct list of items that only need to be defined once (at compile time). Enums are helpful to programmers as a lightweight reference to some value: instead of defining a list of constant variables to compare to, you can use an enum, and get Intellisense support to make sure you don't accidentally use a wrong value.

Got any .NET Framework Question?