Difference between ref and out parameters

Difference between ref and out parameters

23 May 2024
Intermediate
314K Views
6 min read
Learn with an interactive course and practical hands-on labs

Free C# Course Online

ref and out parameters: An Overview

Ref and out parameters are used to pass an argument within a method. Actually, Both parameters indicate that an argument/parameter is passed by reference. By default, parameters are passed to a method by value. By using these keywords we can pass a parameter by reference. In this C# Tutorial, We will explore the ref and out which will include what are ref and out parameters in C#, The difference between ref and out parameters, and when to use ref and out parameters.

Ref

  • The ref keyword is used to pass an argument as a reference.
  • This means that when the value of that parameter is changed in the method.
  • It gets reflected in the calling method.
  • An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.

Out

  • The out keyword is also used to pass an argument like the ref keyword.
  • But the argument can be passed without assigning any value to it.
  • An argument that is passed using an out keyword must be initialized in the called method before it returns back to the calling method.

Program with the ref and out keyword

Let's Elaborate ref and out parameters in C# Compiler with its example:

 public class Example
{
 public static void Main() //calling method
 {
 int val1 = 0; //must be initialized 
 int val2; //optional

 Example1(ref val1);
 Console.WriteLine(val1); // val1=1

 Example2(out val2);
 Console.WriteLine(val2); // val2=2
 }

 static void Example1(ref int value) //called method
 {
 value = 1;
 }
 static void Example2(out int value) //called method
 {
 value = 2; //must be initialized 
 }
}

 

Output

 
1
2
    

Note

  1. Do not be confused with the concept of passing by reference and the concept of reference type. These two concepts are not the same.

  2. A value type or a reference type can be passed to the method parameter by using the ref keyword. There is no boxing of a value type when it is passed by reference.

  3. Properties cannot be passed to ref or out parameters since internally they are functions and not members/variables.

Read More - C Sharp Interview Questions

Ref and out-in method overloading

Both ref and out cannot be used in method overloading simultaneously. However, ref and out are treated differently at run-time but they are treated same at compile time (CLR doesn't differentiates between the two while it created IL for ref and out). Hence methods cannot be overloaded when one method takes a ref parameter and other method takes an out parameter. The following two methods are identical in terms of compilation.

 class MyClass
{
 public void Method(out int a) // compiler error “cannot define overloaded”
 {
 // method that differ only on ref and out"
 }
 public void Method(ref int a) 
 {
 // method that differ only on ref and out" 
 }
} 

However, method overloading can be done, if one method takes a ref or out argument and the other method takes a simple argument. The following example is perfectly valid to be overloaded.

 class MyClass
{
 public void Method(int a) 
 {

 }
 public void Method(out int a)
 {
 // method differ in signature.
 }
}

Ref Vs Out: The Difference

 difference between ref and out parameters in C#
Conclusion:

I hope you will enjoy the ref and out keywords while programming with C#. I would like to have feedback from my blog readers. Your valuable feedback, questions, or comments about this article are always welcome. Also, consider our C# Programming Course for a better understanding of C# concepts.

FAQs

It is nothing but The address (not the value) of an argument is automatically passed to the function.

a ref parameter must be initialized before it is passed, while an out parameter does not need to be.

In a method signature and in a method call, to pass an argument to a method by reference

It is used for the passing the arguments to methods as a reference type

Take our Csharp skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core Certification TrainingNov 08MON, TUE, WED, THU, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
Azure Developer Certification TrainingNov 09SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
.NET Solution Architect Certification TrainingNov 09SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Microservices Certification TrainingNov 09SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Microsoft Azure Cloud Architect with AINov 10SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification TrainingNov 10SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Angular Certification TrainingNov 17SAT, SUN
Filling Fast
06:00PM to 08:00PM (IST)
Get Details
Software Architecture and Design TrainingNov 24SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
.NET Solution Architect Certification TrainingNov 24SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this