C# Data Types:
Data
types specify the type of data that a valid C# variable can hold. C# is a
strongly typed programming language because in C#, each type of data is
predefined as part of the programming language and all constants or variables
defined for a given program must be described with one of the data types.
In
C#, data types are categorized based on how they store their value in the
memory.
C# includes following categories of data types:
· Value
type
· Reference
type
Value Type:
Value
type variables can be assigned a value directly. They are derived from the
class System.ValueType. Value type holds both data and memory on the same
location. A Value Type stores its contents in memory allocated on the stack.
When you created a Value Type, a single space in memory is allocated to store
the value and that variable directly holds a value. If you assign it to another
variable, the value is copied directly and both variables work independently.
Predefined datatypes, structures, enums are also value types, and work in the
same way.
For Example:
Consider integer
variable int i = 100;
In C#, value types
are further divided into simple types, enum types, struct types, and nullable
value types.
- Simple Types
- Signed integral: sbyte, short, int, long
- Unsigned integral: byte, ushort, uint, ulong
- Unicode characters: char
- IEEE floating point: float, double
- High-precision decimal: decimal
- Boolean: bool
- Enum types
- User-defined types of the form enum E {...}
- Struct types
- User-defined types of the form struct S {...}
- Nullable value types
- Extensions of all other value types with a null value
Reference Type:
The
Reference Data Types will contain a memory address of variable value. It do not
contain the actual data stored in a variable, but they contain a reference to
the actual data. Reference Type variables are stored in a different area of
memory called the heap. Assigning a reference variable to another variable
doesn't copy the data. Instead it creates a second copy of the reference, which
refers to the same location of the heap as the original value.
For example:
In C# reference types are further divided into class types, interface
types, array types, and delegate types.
- Class types
- Ultimate base class of all other types: object
- Interface types
- User-defined types of the form interface I {...}
- Array types
- Single- and multi-dimensional, for example, int[] and int[,]
- Delegate types
- User-defined types of the form delegate int D(...)
Prof. Shardul P. Patil
profshardulp.patil@gmail.com