Compartilhar via


Value Types (C# Reference)

The value types consist of two main categories:

Structs fall into these categories:

Main Features of Value Types

Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself.

All value types are derived implicitly from the System.ValueType.

Unlike with reference types, you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces.

Unlike reference types, a value type cannot contain the null value. However, the nullable types feature does allow for value types to be assigned to null.

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

Main Features of Simple Types

All of the simple types -- those integral to the C# language -- 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 (C# Reference).

Constant expressions, whose operands are all simple type constants, are evaluated at compilation time.

Simple types can be initialized by 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 they are used. For example, you might declare a local variable without initialization as in the following example:

int myInt;

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

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

This statement is equivalent to the following statement:

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

You can, of course, have the declaration and the initialization in the same statement as in the following examples:

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 about 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 default constructor for the struct.

After this call, the struct is considered to be definitely assigned; that is, all its members are initialized to their default values.

For more information about the new operator, see new.

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

See Also

Reference

C# Keywords

Reference Types (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference

Types (C# Reference)

Reference Tables for Types (C# Reference)