.NET Framework Custom Types

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

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?