Skip to content

Latest commit

 

History

History
69 lines (45 loc) · 4.09 KB

value-types.md

File metadata and controls

69 lines (45 loc) · 4.09 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
Value types vs reference types, kinds of value types, and the built-in value types in C#
Value types
01/22/2020
cs.valuetypes
value types [C#]
types [C#], value types
C# language, value types
471eb994-2958-49d5-a6be-19b4313f80a3

Value types (C# reference)

Value types and reference types are the two main categories of C# types. A variable of a value type contains an instance of the type. This differs from a variable of a reference type, which contains a reference to an instance of the type. By default, on assignment, passing an argument to a method, and returning a method result, variable values are copied. In the case of value-type variables, the corresponding type instances are copied. The following example demonstrates that behavior:

[!code-csharpcopy of values]

As the preceding example shows, operations on a value-type variable affect only that instance of the value type, stored in the variable.

If a value type contains a data member of a reference type, only the reference to the instance of the reference type is copied when a value-type instance is copied. Both the copy and original value-type instance have access to the same reference-type instance. The following example demonstrates that behavior:

[!code-csharpshallow copy]

Note

To make your code less error-prone and more robust, define and use immutable value types. This article uses mutable value types only for demonstration purposes.

Kinds of value types and type constraints

A value type can be one of the two following kinds:

  • a structure type, which encapsulates data and related functionality
  • an enumeration type, which is defined by a set of named constants and represents a choice or a combination of choices

A nullable value type T? represents all values of its underlying value type T and an additional null value. You cannot assign null to a variable of a value type, unless it's a nullable value type.

You can use the struct constraint to specify that a type parameter is a non-nullable value type. Both structure and enumeration types satisfy the struct constraint. You can use System.Enum in a base class constraint (that is known as the enum constraint) to specify that a type parameter is an enumeration type.

Built-in value types

C# provides the following built-in value types, also known as simple types:

All simple types are structure types and differ from other structure types in that they permit certain additional operations:

  • You can use literals to provide a value of a simple type. For example, 'A' is a literal of the type char and 2001 is a literal of the type int.

  • You can declare constants of the simple types with the const keyword. It's not possible to have constants of other structure types.

  • Constant expressions, whose operands are all constants of the simple types, are evaluated at compile time.

A value tuple is a value type, but not a simple type.

C# language specification

For more information, see the following sections of the C# language specification:

See also