Data Haunting

Easy Solutions Destination


Difference between ref and out in c#

In C# many times we have used ref and out parameters. In this article we will compare ref and out with example and will try to find out difference between reference and out keywords in c# and will conclude which is best among ref vs out.

Define ref and out parameters

Ref– is used to pass arguments as a reference. That means if we pass arguments to method as reference and change the value in method than it will also reflect in calling method. Ref parameters must be initialized before passing arguments as a reference to the method.
Out– is also used to pass arguments as a reference, but for out parameter it is not necessary to initialized any value before passing argument while it is compulsory that out parameter must be intialized in the calling method before it returns to calling method.

Ref and out example in c#

Practically both ref and out pass argument to method and returns from that method, even though there are some differentiation as

  1. out parameter does not need to initialize while ref parameter must be initialized before passing arguments to the method
  2. For out parameter in method it is compulsory to initialize before returning from method while for ref parameter it is not compulsory to initialize in the method

See both Ref and Out parameter pass argument as a reference, so when you change value of Ref and Out parameter in called method, it will automatically change in calling method. In above example we have change value of refVrb parameter to =”refB” from =”refA” in called method so it will automatically reflect in calling method and will change value of refVrb variable to = “refB”.

Ref vs Out in c#

  1. We have to use out unless we need to use ref .
  2. Ref is two way that is in and out while out is only out.
  3. Ref tells the compiler that object is already initialized before entering into method or function. It is required to initialize ref object before entering into method or function while out object is initialized after entering into method or function. For out object it is required that it must be initialized into method or function
  4. As ref parameter initialize before entering into method or function so it occupies memory initially while out parameter occupies memory when it is called by method or function and initialized in that method .
  5. Reference data needs to be initialized before passing to method which makes a very huge difference in the context of marshaling(e.g. it starts another process and can be very costly). So avoid to marshaling initial value, we have to use out parameter unless we need ref parameter .
  6. For best performance if possible than we have to use out parameter rather than reference parameter.

Ref and Out in method overloading

Ref and Out both can not be used simultaneously in method overloading because ref and out treated different on run-time but both treated same on compile time. So method can not be overloading when one method takes ref and other takes an out parameter because both methods are identical(completely same) in compilation and will throw compile time error differs from another method only on ref and out for following programming.

We can short out problem of overloading if one method takes simple argument and other method takes either a ref or out parameter. You can overload method by programming as below.

What is your opinion ?

Here we have seen Out vs Ref parameter in c# and also it’s behavior in method overloading. I hope this concept will be very useful to you while you programming in c# and you will enjoy it. If you have any query and question, you can comment or you can mail me.


Leave a Reply