C# Value Types and Reference Types¶
Overview¶
There are two kinds of types in C#:
- reference types
- value types
Variables of value types directly contain their data. As a result, the copy operation or assignment operation for variables of value types will create a new variable.
Variables of reference types store references to their data. As a result, the copy operation creates a new variable that references to the same data
Reference Types¶
Following keywords are used to declare reference types:
class
interface
delegate
record
And followings are the built-in reference types:
dynamic
object
string
Value Types¶
A value type can be one of the two following kinds:
- structure type (typically defined with
struct
keyword) - enumeration type (defined with
enum
keyword)
Following are the built-in value types (also known as simple types):
- Integral numeric types
- Floating-point numeric types
bool
char
Additional notes:
- All simple types are structure types.
- simple types differ from the other structure types in the following ways:
- You can use literals to provide a value of a simple type
- e.g.
'A'
for a variable of typechar
, or123
for a variable of typeint
- e.g.
- You can apply
const
keyword to a variable of simple type; you cannot do so for the other structure types. - Constant expressions whose operands are all constants of the simple types are evaluated at compile time.
- You can use literals to provide a value of a simple type