Tuesday, March 16, 2010

Default Values

Each value type has a constructor that initializes it to its default value. The default value for each type is shown in below: -
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 evaluation 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 initialization all value – type fields to their default values and reference – type fields to null.
uint    0
ulong    0
ushort    0
When using structs, we 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):
struct Point
{
    int x;
    int y;
}
In order to create an object of the type 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.

No comments:

Post a Comment