Data Types
In C#, there are two types of data:-
- Value Types: Variables that store data values on the stack.Value types: Variables that store data values on the stack.
- Reference Types: Variables that store memory addresses of the data stored on the heap.
There is also a third type of data, the pointer type, which is used in writingReference types: Variables that store memory addresses of the data stored on the heap.
Built-in Data Types
C# built-in data types
| |
C# Type
|
.NET Type
|
bool
|
System.Boolean
|
byte
|
System.Byte
|
sbyte
|
System.SByte
|
char
|
System.Char
|
decimal
|
System.Decimal
|
double
|
System.Double
|
float
|
System.Single
|
int
|
System.Int32
|
uint
|
System.UInt32
|
long
|
System.Int64
|
ulong
|
System.UInt64
|
object
|
System.Object
|
short
|
System.Int16
|
ushort
|
System.UInt16
|
string
|
System.String
|
Value Types Value types include the numeric types (integers, floats, and so forth) and the Boolean type (bool). They also include user-defined types such as structs and enumerations. The following table shows the C# value types and a description of each type.bool). They also include user-defined types such as structs and enumerations. The following table shows the C# value types and a description of each type.
C# Type Description
| |
bool
|
Boolean
|
byte
|
Unsigned integral
|
char
|
Unsigned integral
|
decimal
|
Signed numeric
|
double
|
Signed numeric
|
enum
|
Enumeration
|
float
|
Signed numeric
|
int
|
Signed integral
|
long
|
Signed integral
|
sbyte
|
Signed integral
|
short
|
Signed integral
|
struct
|
User-defined structure
|
uint
|
Unsigned integral
|
ulong
|
Unsigned integral
|
ushort
|
Unsigned integral
|
Value types are stored on the stack, which is the region in memory used to store local variables and their data. All value types are implicitly derived from the Value Type class in the System namespace. Value types, however, cannot be inherited.stack, which is the region in memory used to store local variables and their data. All value types are implicitly derived from the Value Type class in the System namespace. Value types, however, cannot be inherited.Value Type class in the System namespace. Value types, however, cannot be inherited.
Variable Initialization
To use a variable you must first initialize it with a value, as shown in the following examples:
int myValue = 123;
In the first example, we initialized the variable myValue with the value 123. In the second example, it is initialized with zero. In the third example, the variable is also initialized with zero by using the keyword new, which calls the default constructor (a constructor is a method that initializes annew, which calls the default constructor (a constructor is a method that initializes anconstructor (a constructor is a method that initializes an instance of a class) that initializes the variable with its default value —zero in this case. This means that the last two statements are equivalent.
int myValue = 0;
int myValue = new int();
Default Values
When using structs, you instantiate the struct by using the keyword new, which initializes the instance members with the default values. Following is an example of a struct that represents a point at the coordinates (x,y):new, which initializes the instance members with the default values. Following is an example of a struct that represents a point at the coordinates (x,y):
struct Point
In order to create an object of the type Point, use a statement like this:Point, use a statement like this:
Point myPoint = new Point();
This statement initializes all the members of the object (x and y in this case) with the value 0. This is called definite assignment of the struct. We’ll talk more about structs later in this book.definite assignment of the struct. We’ll talk more about structs later in this book.
Reference Types
A reference-type variable does not contain the data itself; it actually contains the memory address of the data. It is similar to pointers and references in C++, but much easier to use. The variable itself lives on the stack, like a value-type variable, but points to its data that lives on the heap.
-------------------------------------------------------------------------------------------------------
Note : The heap is the region in memory that stores the data pointed to by the reference-type variables.The heap is the region in memory that stores the data pointed to by the reference-type variables.
-------------------------------------------------------------------------------------------------------
The C# Reference Types
The following are the C# reference types:
- ClassClass
- InterfaceInterface
- DelegateDelegate
- ObjectObject
- StringString
{
int x;
int y;
}
It is possible to convert value types to reference types by using
Boxing and Unboxing
The boxing operation is accomplished by assigning the value-type variable to a variable of the type object:
int myInt = 123;
object myObj = myInt; // boxing
This means moving the value 123 from the stack to the heap, as shown in the following figures.
Memory before boxing.
Memory After Boxing
In order to convert the variable back to a value type you use the unboxing operation, which is performed by casting the reference-type variable with (int). The following statement assigns the value pointed to by myObj to a new value-type variable, yourInt:
yourInt = (int) myObj; // unboxing
This statement creates a new value-type variable that contains the same value, 123, as shown in below figure :
Memory After Unboxing
Notice that you can use the same variable, myInt, instead of using a third variable, yourInt, in the unboxing operation.
Note : Boxing is necessary in cases when you would like to use value types in collections (explained in Chapter 11) where items of the collection are of the type object. Unboxing is also used in accessing the value-type contents of an object.
For Example :-
// Boxing and Unboxing
using System;
public class BoxingAndUnboxing
{
static void Main()
{
// Declare a value type:
int myInt = 123;
// Boxing and changing the value:
object myObj = myInt + 321;
// Unboxing:
int yourInt = (int) myObj;
Console.WriteLine("myInt = {0}", myInt);
Console.WriteLine("myObj = {0}", myObj);
Console.WriteLine("yourInt = {0}", yourInt);
}
}
Each value type has a constructor that initializes it to its default value. The default value for each type is shown in the following table.
The default values for each value type
Type Default Value
| |
bool
|
FALSE
|
byte
|
0
|
char
|
\0'
|
decimal
|
0.0M or 0.0m
|
double
|
0.0D or 0.0d
|
enum
|
The value resulting from evaluating the expression
E(0), where E is
|
the enumeration identifier.
| |
float
|
0.0F or 0.0f
|
int
|
0
|
long
|
0L or 0l
|
sbyte
|
0
|
short
|
0
|
struct
|
The value resulting from initializing all value-type fields to their
|
default
|
values and reference-type fields to null.
|
uint
|
0
|
ulong
|
0
|
ushort
|
0
|
No comments:
Post a Comment