DEV Community

Cover image for Call by Value vs. Call by Reference in C#
Shreyans Padmani
Shreyans Padmani

Posted on

Call by Value vs. Call by Reference in C#

One of the most frequently asked interview questions in C# is:

πŸ‘‰ What is Call/Pass by Value vs. Call/Pass by Reference ❓

Many developers, especially beginners, often respond with:
❌ Primitive types are passed by value, and reference types are passed by reference.

⚠ This is a misconception!

In this blog, I’ll break down the real difference between Call by Value and Call by Reference in C# with clear explanations and examples. Let's dive in!

Call by Value in C#

By default, in .NET, everything (value types & reference types) is passed by value, unless explicitly stated otherwise.

How It Works
When you pass a variable by value, a copy of the original variable is passed. Any modifications made inside the method won’t affect the original variable.

Image description

Call by Reference in C#

To pass a variable by reference, you must explicitly use the ref or out keyword. This means the method receives a reference to the original variable instead of a copy, and changes will affect the original value.

Image description

Final Takeaways

βœ” By default, everything in C# is passed by value, whether it’s a value type or reference type.
βœ” To modify the original variable, you must use ref or out.
βœ” Reference types are NOT automatically passed by reference. Their reference is passed by value unless explicitly stated otherwise.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post