Home > csharp > Ref and Out parameters in C#

Ref and Out parameters in C#

In this C# tutorial I will explain about main difference between ref and out parameters with simple C# examples.ref and out  parameters allows us to pass the parameters by reference instead of Value in C# language.

The simple way of declaring methods with ref and out parameters are as follows

 

Now we will understand the difference between ref and out parameters by writing a small program

 

Out Parameter in C#:

If you execute the above program you will get following errors

  • The out parameter ‘a’ must be assigned to before control leaves the current method
  • Use of unassigned out parameter ‘a’

i.e., If you are using “out” parameters you must initialize the out parameter in CalledMethod (MethodOut). It’s not necessary to initialize the out parameter before calling the method.Even though you initializes the variable, called method cannot read it. Called method should write the “out” parameter before returning to calling method.

We can have only one return type to a particular Method. So “out” parameter is useful when you want a method to return multiple values.

 

In the above example we are updating “a” to Database the method will return true or false. If its failed we want the reason why it got failed. In that case we can “out” parameter as reason and assign corresponding error to that “out” parameter.(You must assign out parameter that’ y I assigned to Empty in the success case)

Ref Parameter in C#:

Now we will come to “ref” parameter . I slightly modified the starting program as shown below.

 

The code will throw following error

  • Use of unassigned local variable ‘aref’

That means you must assign the value before calling the function.(Same for normal variables but the difference is ref parameter called by reference and normal parameters called by Value).

This is the main difference between ref and out parameter.Out parameters is not required to assign the value before calling the function where as ref parameter should assign the value like normal parameters.

So When to use “ref” parameters If you want to do manipulations on parameters (For example swapping two variables) we can use the ref parameters. Please see the below example

 

Few Similarities between ref and out Parameters:

  • Both allows us to pass parameters by reference instead of by Value
  • For CLR(Common Language Run time) both ref and out parameters are identical that means same Intermediate code will generate for both keywords. and the metadata also same except for one bit which specifies whether you specified out or ref when declaring the method.
  • The difference is that the C# Compiler ensures that we write the correct code.(That’s why compile time error will come when didn’t initialize the ref parameter).

A ref or Out parameter cannot have default values as normal parameters. The following code snippet will not compile

 

Overloading of Methods with ref and out Parameters in C#:

This is an interesting question just execute the following code snippet

 

This will throw the following error

  • Cannot define overloaded method ‘Add’ because it differs from another method only on ref and out

As I mentioned in similarities (second point) as the CLR treat both keywords are same their Metadata representation of the method signature and everything will be identical. It’s not correct to declare a method with same signatures and parameters.

It’s again C# Compiler responsibility to ensure we write the correct code(third point in similarities) So that’s why it’s returned an error.

I hope you’ve enjoyed this article and that it gives you more ideas on ref  and out parameters in C# and similarities and differences between them.If so share this post with your friends and also join our mailing list

 

Arunkumar Gudelli
I am a Simple Guy, Standing Infront of the World, asking you to Follow Me. Don't miss any posts from Me,Subscribe to the RSS Feed. You can connect me via @twitter or @facebook or Google+ or e-mail.
http://www.arungudelli.com