All About C# Value Types & Reference Types
By: Lanka Rathnayaka
Tags: Programming, Tutorial, Aspnetcore, Aspnet, Programming Languages

Photo by ThisisEngineering RAEng on Unsplash

Table Of Content

· Introduction

∘ Value Type Reference Type Table

· Value Types

∘ Passing the Value type through methods

· Reference Type

∘ Passing Reference Type Variables in Methods

· Special Scenario of String

· Don't get Confused

· Can Reference/Value types be Null?

This is another key area that c# developers should be really clear about. Learning these 2 concepts helped me a lot when reading C# codes and understanding what's going on. So I thought of sharing what I learned with my readers so you won't get confused like “how did that thing change I didn't even open that file?”

Introduction

Getting straight to the point, Value Types and Reference Types are the main 2 data types in C#. This value type, reference type separation is done based on the way how each type stores its value in memory. If you have a clear picture of how each variable type is stored inside memory(RAM) you may not have to bother about value/reference confusion in your lifetime !!!.

Before going forward let's see the data types in each of the two categories.

Value Type Reference Type Table

+-------------+-----------------+

| Value Types | Reference Types |

+-------------+-----------------+

| bool | String |

| byte | Arrays |

| char | Class |

| decimal | Delegate |

| double | |

| enum | |

| float | |

| int | |

| long | |

| sbyte | |

| short | |

| struct | |

| uint | |

| ulong | |

| ushort | |

+-------------+-----------------+

Variables we use in our program are stored inside the memory(RAM). There is a separate block of space inside the memory allocated to store each variable. And each of these separate blocks of space has a location name/id for identification. we call it a memory address. Example memory address 0x239010.

How this memory is allocated and created is another world that you should explore. Now is not the time for that.

Value Types

Value type variable stores its value within its own memory location. So the variable directly has its assigned value.

For example, let's see how int number =156 is stored as a value type.

The system stores the value 156 in the same memory space allocated to the variable name number .

This illustration shows how the value is stored in the same memory location 0x239110 with the variable name number.

Value type -Memory Allocation

You will get to feel this more when you get to know about the Reference type memory allocation.

Passing the Value type through methods

When writing methods and functions we pass parameters. Parameters have a variable type. So if you are passing a value type as a parameter you need to know what happens inside.

When passing a value type as a parameter through a method, the System creates a separate copy and sends it. If the value got changed inside the method it will not affect the original variable.

In the below code on line 16 inside the main method, it prints 100.

Then pass that variable as a parameter to MySalary() method where mysalary variable gets modified.

As the value type passes a copy of the variable to calling methods, the original friendsSalary value inside the main method does not change.

Therefore it prints 300 on line 18 and 100 on lines 20 and 16.

Reference Type

Reference type variables do not store the value directly with the variable name. It stores the memory location where the actual value is stored.

So in reference type actual value is stored in a separate place and the created variable stores only the memory address of the actual values.

From the below illustration you can see variable name stores the address of the actual value’s memory location. Actual values are stored in a separate memory location(0x60001).

Storing Reference Type Variables

Passing Reference Type Variables in Methods

When we are passing reference types as parameter values there are some things we should understand.

When passing reference types it doesn't create a copy as in value types it directly passes the stored reference address.

In the below code, std1 stores only the reference address of the Object, and it gets passed to the method UpdateCourse . So if we changed the variable values inside the method it gets reflected inside other methods as well.

In here changing the course name inside UpdateCourse method got reflected in the Main method as well.

You can see how the same Console.WriteLine("Course: "+std1.Course); has printed 2 different values before and after the UpdateCourse() method call.

That's how objects get passed and modified as reference type variables

Special Scenario of String

As I mentioned in the earlier table, string is a reference type. But it does not act like a normal reference type. Because strings are immutable. That means the string value cannot be modified. If we tried to modify a string it will create a new variable inside the memory. And do the change to the new variable. Changes will not be reflected in the original string value.

In the code example below you can see inside the main method, we tried to modify the string value “name” by passing it to ModifyingString() method. The change was successful but the change did not reflect back to the main method like other reference types variables.

Don't Get Confused

It's true that in the first example std1.StudentName std2.StudentName are all string values. But remember we passed the relevant object to the method as parameters, not the direct string as in the second example.

Can Reference/Value types be Null?

Yes if you think closely reference type does not directly hold the variable values. So they can remain without referring to any object. But Value type variables cannot stay as null because it directly holds the value. But in

From C# 2.0 nullable types were introduced where you can assign null to a value type.

So that's it for today hope you enjoyed and got something out of the article.