Home  >  Article  >  Backend Development  >  Value types and reference types in C#

Value types and reference types in C#

王林
王林forward
2023-09-03 16:45:321026browse

C# 中的值类型与引用类型

Value types and references are both types in C# -

Value type

Value type variables can be assigned directly. They are derived from the System.ValueType class. Value types contain data directly. When the int type is declared, the system allocates memory to store the value.

Value type variables are stored on the stack.

For example, int, char and float, which store numbers, letters and floating point numbers respectively.

Reference type

It refers to a memory location. Using multiple variables, reference types can refer to memory locations. If data in a memory location is changed by one of the variables, the other variable automatically reflects this change in value.

Reference type variables are stored in the heap.

The example built-in reference type is -

  • object
  • dynamic
  • string

The above is the detailed content of Value types and reference types in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete