Value type and Reference type in C#

In computer programming, data types can be divided into two categories: value types and reference types. Value type holds a data value with in its own memory space. In most cases, the value is often stored in the stack. Reference type holds a pointer or memory address that holds the data. The pointer is often stored in stack and the actual data is stored in the heap.

In c#, some build-in value types are byte, short, int, long, float, double, decimal, char, bool. You also can create custom value type by using the key word struct instead of class. A value type is initialized when it is declared, because it has its own space once declared so it can’t be null, and it can’t be null. Reference type variables hold a reference to data, and its real data is stored in the data heap. Variables of all class instances and strings are reference types. When a variable of class is declared, only a small piece of memory is allocated in the stack (sometimes heap) to hold an address, and no memory space on the heap is allocated for it at this time, so it is empty and null until new keyword is used to create an instance of a class.

Examples showing how memories is used for value type (int) and reference type (string):

In other programming languages like Java, you have the primitive types (int, boolean, char, byte…), and everything else extends the object class and therefore is an object. Different from java, C# is a strict object-oriented language. That means that Everything is an Object in C#. All values are stored as objects or as members of objects. Everything is a part of a class or structure, and much of what you work with are instances of these classes or structures.

We often make mistakes when we use struct. Often many people use it as a class, which is the wrong way to understand it. Because it is a value type, the value of a variable is completed by copying when we assign the structure to other variables. For example, a and b are both structures, a is assigned to b, and when b changes the data, it is found that the data of a is not changed.

Many people explain value types and reference types as the difference between stack and heap memory allocation, which is wrong. Stack is mainly used for memory within the function life cycle, while heap is more of unordered memory that can be released at any time. Therefore, the value type can be in the heap or the stack, and the pointer part of the reference type can also be in the stack and the heap. The difference is that the memory blocks pointed to by the reference type are all in the heap. This is convenient for memory control and garbage collection.





  1. Brown, Erik E. (2006). Windows Forms in Action. Shelter Island, New York: Manning. p. 703. ISBN 978-1-932-39465-8.
  2. Stephens, Rod (2014). C# 5.0 Programmer’s Reference. Indianapolis, Indiana: John Wiley & Sons. p. 57. ISBN 978-1-118-84728-2.

扫一扫,分享到微信

微信分享二维码
  • © 2020-2022 Xiunan Yu
  • Powered by Hexo Theme Ayer

请我喝杯咖啡吧~

支付宝
微信