Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Value types (C# reference)
In this articleValue 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:
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:
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 constraintsA value type can be one of the two following kinds:
A nullable value type You can use the Built-in value typesC# 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:
A value tuple is a value type, but not a simple type. C# language specificationFor more information, see the following sections of the C# language specification:
See also
FeedbackSubmit and view feedback for Is a value given to a method?An argument is a value that is passed into a method when the method is called. A parameter variable is a variable that is declared in the method header, and receives the value of an argument when the method is called.
What does a value supplied to the method called?The value supplied to a method is typed in the parentheses of the method and it is called an argument.
Is a variable that receives an argument which is passed into a method?A parameter is a variable that receives an argument that is passed into a function.
What is argument vs parameter?Note the difference between parameters and arguments: Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.
|