User-Defined Value Types in .NET

You can define a class and declare two variables x and y to store the values of the x and y coordinates on the screen. However, for the CLR to use a class to store two simple values involves a performance overhead. To avoid this, you can create user-defined value types in the .NET Framework, such as structure, constant and enumeration. You can use the following user-defined value types to create custom value types that make the .NET Framework fully extensible"

Structure

The user-defined value type, structure, is similar to a class. It represents a data structure that contains member variables and functions. You will find structures useful when you need to store logically related values in one value type.

Constant

A constant is like a normal value type variable that holds a value on the stack. However, you assign a value to a constant at the time of its declaration, and its value cannot be changed or reassigned. You can create constants by using the const keyword in c#. Use constants to represent values that don not change.

Enumeration

An enumeration is a list of named integer constants. Each constant in an enumeration is mapped to an integer value, which begins at zero, by default and increments the subsequent constant values by one. Yu use enumerations when you need to select a choice from a fixed number of choices at compile time. You can use enumerations to define a fixed set of values from a property.

Ebook Download
View all
Learn
View all