Export (0) Print
Expand All
9 out of 12 rated this helpful - Rate this topic

Value Types

The value types consist of two main categories:

The struct types contain the user-defined struct types and the following built-in simple types:

In addition to explaining each category of the value types, the following topics are discussed in this section:

Main Features of Value Types

A variable of a value type always contains a value of that type. The assignment to a variable of a value type creates a copy of the assigned value, while the assignment to a variable of a reference type creates a copy of the reference but not of the referenced object.

All value types are derived implicitly from the Object class.

Unlike reference types, it is not possible to derive a new type from a value type. However, like reference types, structs can implement interfaces.

Unlike reference types, it is not possible for a value type to contain the null value.

Each value type has an implicit default constructor that initializes the default value of that type. For information on default values of value types, see Default Values Table.

Main Features of Simple Types

All of the simple types are aliases of the .NET Framework System types. For example, int is an alias of System.Int32. For a complete list of aliases, see Built-in Types Table.

Constant expressions, whose operands are all simple type constants, are evaluated at compilation time. For more information, see 7.15 Constant expressions.

Simple types can be initialized using literals. For example, 'A' is a literal of the type char and 2001 is a literal of the type int.

Initializing Value Types

Local variables in C# must be initialized before being used. Therefore, if you declare a local variable without initialization like this:

int myInt;

you cannot use it before you initialize it. You can initialize it using the following statement:

myInt = new int();  // Invoke the default constructor for the type int

which is equivalent to:

myInt = 0;          // Assign an initial value, 0 in this example

You can, of course, have the declaration and the initialization in the same statement like this:

int myInt = new int();

–or–

int myInt = 0;

Using the new operator calls the default constructor of the specific type and assigns the default value to the variable. In the preceding example, the default constructor assigned the value 0 to myInt. For more information on values assigned by calling default constructors, see Default Values Table.

With user-defined types, use new to invoke the default constructor. For example, the following statement invokes the default constructor of the Point struct:

Point p = new Point();   // Invoke the default constructor for the struct

After this call, the struct is considered to be definitely assigned; that is, all of its members are initialized to their default values. For more information, see 5.3 Definite assignment.

For more information on the new operator, see new.

For information on formatting the output of numeric types, see Formatting Numeric Results Table.

See Also

C# Keywords | Types | Types Reference Tables | Reference Types

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.