Data Types and Value Types in .NET

Introduction

Value types are system types that contain the actual data assigned to them. You can use value types such as System.Char, System. Int32 and System.Single in the .NET Framework to store characters, integers and floating-point numbers, respectively.

Working with value types

When you make a function call during program execution, all value type variables of the calling function are pushed onto the program stack, because value types are stored in the stack section of the computer. As soon as the function returns the control to the caller, the value types of the function are released from the stack and the memory used by value types is released automatically. The size and lifetime of variables on the stack are determined at compile time. As a result, the allocation and deallocation of variables on the stack is rapid.

When you pass a value type from one function to another, a copy if the value is passed instead of its reference; you can modify the values in both functions independently. This is useful when you want to pass only a value from a function, say A, to another function B, but do not want B to alter the original copy of the value being passed.

The .NET Framework provides two types of value types, built-in and user-defined.

Built-in Value Type

Built-in value types are also referred to as simple or primitive value types. Built-in value types, such as System. Int32, are defined in the Common Language Infrastructure (CLI). As a result, these value types are the most efficiently managed value types in the .NET Framework. Each built-in type in the .NET Framework is directly mapped to a corresponding primitive type in a programming language.

User-defined value types

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 a 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 do not change.

Enumeration

An enumeration is a list of named integer constants. Each constant in an enumeration is mapped to an integer value, that begins at zero, by default and increments the subsequent constant values by one. You 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.