Value type vs Reference type

suggest change

Syntax

Remarks

Introduction

Value types

Value types are the simpler of the two. Value types are often used to represent data itself. An integer, a Boolean or a point in 3D space are all examples of good value types.

Value types that you define are declared with the struct keyword.

Generally speaking, we have two keywords that are used to declare value types:

Reference types

Reference types are slightly more complex. Reference types are traditional objects in the sense of Object Oriented Programming. So, they support inheritance (and the benefits thereof) and also support finalizers.

In C# generally we have these reference types:

New reference types (classes) are declared with the class keyword.

Major Differences

The major differences between reference types and value types can be seen below.

Value types are stored inline; reference types live on the heap

When you use a value type in C#, such as an int, the variable stores that value directly. A local value may live on the stack; a value stored in a class, or a boxed value, lives on the heap. The program uses that variable to refer directly to that value. If you say int mine = 0, then the variable mine refers directly to 0, which is efficient. However, reference types actually hold (as the name suggests) a reference to the underlying object, this is akin to pointers in other languages such as C++.

You might not notice the effects of this immediately, but the effects are there, are powerful and are subtle. See the example on changing reference types elsewhere for an example.

This difference is the primary reason for the following other differences, and is worth knowing.

Value types don’t change when you change them in a method, reference types do

When a value type is passed into a method as a parameter, if the method changes the value in any way, the value is not changed. In contrast, passing a reference type into that same method and changing it will change the underlying object, so that other things that use that same object will have the newly changed object rather than their original value.

See the example of value types vs reference types in methods for more info.

What if I want to change them?

Pass a value type with the ref keyword when the method must change the caller’s value. A reference type can already be mutated through its reference; ref also lets the method replace the caller’s reference. See the example on passing by reference for an example.

Value types cannot be null, reference types can

Pretty much as it says, you can assign null to a reference type, meaning the variable you’ve assigned can have no actual object assigned to it. In the case of value types, however, this is not possible. You can, however, use Nullable<T> to allow your value type to be nullable, if this is a requirement, though if this is something you are considering, think strongly about whether a class is a better approach, if it is your own type.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents