Author avatar

Matt Ferderer

C# - Passing a Reference vs. Value

Matt Ferderer

  • Jun 12, 2019
  • 4 Min read
  • Jun 12, 2019
  • 4 Min read
Programming Languages


In this guide, we will look at the difference between passing reference types and value types as parameters in C# methods. Without understanding this, it can be easy to change the wrong variable and spend hours trying to figure out what happened.

Reference Types

A reference type variable contains a reference to data stored in memory, also known as the heap. The heap is most often used for data that has a longer life. You can have more than one variable point to the same referenced data. Objects are an example of a reference type.

1var a = new Student();
2var b = a;

In the above example, both variables a and b will point to the same student object in memory. If we change a, we will also change b. This is because a and b are not storing the data but a reference to the location where the data is stored.

Value Types

A value type variable is immutable data which contains the data, instead of a reference to it. Value types often have short lives. They are typically stored in memory in an area known as the stack. The stack is where data that does not need to exist for long lives. Structs, Int32, DateTime, and Double are examples of value types.

Passing a Reference Type

By default, passing a reference type variable to a method passes a copy of the reference, not the actual data. If you change a value inside the reference type, this will also change the value outside the method as well, since it's pointing at the same reference location in memory. A caveat occurs when you try to assign the variable to a new object inside the method. This will make the variable no longer point at the reference object in memory. Any changes thereafter will not be reflected in the original referenced object. Here's an example:

1class ReferenceTypeExample 
3	static void Enroll(Student student)
4	{
5		student.Enrolled = true; // This changes the student variable that was passed in outside of the method.
6		student = new Student(); // This does not change the student variable outside of the method but creates a new reference. Since student now points to a new reference, the student variable outside of the method is no longer affected after this line.
7		student.Enrolled = false; // This changes the local student inside the method.
8	}
10	static void Main()
11	{
12		var student = new Student
13		{
14			Name = "Susan",
15			Enrolled = false
16		};
18		Enroll(student);
20		// student.Name is still Susan
21		// student.Enrolled is now true
22	}
25public class Student {
26	public string Name {get;set;}
27	public bool Enrolled {get;set;}

Passing a Value Type

When a value type is passed into a method, the value is considered local to the method. Anything done inside the method will not change the original variable.

1class ReferenceTypeExample 
3	static void Enroll(bool enrollmentStatus)
4	{
5		// This will not change any value outside the method.
6		enrollmentStatus = true;
7	}
9	static void Main()
10	{
11		var student = new Student
12		{
13			Name = "Susan",
14			Enrolled = false
15		};
17		Enroll(student.Enrolled);
18		// student.Enrolled still equals false.
19	}
22public class Student {
23	public string Name {get;set;}
24	public bool Enrolled {get;set;}


Knowing these simple techniques can prevent you from making mistakes that change the value of your data.

To learn more about passing reference types, value types, and other types in C# check out Accelerated C# Fundamentals by Scott Allen. Don't forget to try out the C# Path and see where your skills are at and where you can improve!

About the Author

Matt Ferderer is a software developer who tweets, posts and blogs about web development.