01 July 2016 Sudhanshu Ranjan 1.37k

Difference between ref and out parameters with example.

ref and out parameters are used to pass an argument within a method. By using these keywords (ref and out) we can pass a parameter by reference. In this section , you will learn the differences between these two parameters.


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


out is very similar to ref keyword. It is used to pass an arguments 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/function before it return back.

Points to remember

  • Both are used by function/method.
  • In ref, we must have to initialized before use and In out, it must be initialized before return back to calling method.
  • When we use ref, data can be passed bi-directionally but in out data is passed only in a unidirectional way (from the called method to the caller method).
  • Both ref and out are treated differently at run time and they are treated the same at compile time.
  • if same name methods take an argument as ref and the other takes an argument as an out then it cann't be overloaded method. You will get an error at these senario.

Example :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dotnetcube
    class Program
        static void Main(string[] args)
            int val1 = 0; //must be initialized 
            int val2; //optional

            Method1(ref val1);


            Method2(out val2);



        static void Method1(ref int value)
            value = 1;
        static void Method2(out int value) 
            value = 2; //must be initialized here


Ref / Out keyword Vs method Overloading

Point to remember  :  

  • Method overloading can be possible only when one method takes a ref or out argument and the other takes the same argument without ref or out.


You can share at :

About Author
Sudhanshu Kumar Ranjan | Lead Engineer at HCL Technologies | Expertise in Microsoft Technology | Author | Blogger | Programmer
Know More
Connect with him on Facebook | Google+ | LinkedIn | Twitter

Recommended Post

Leave a comment

About Dotnet Cube

It's my blog - Dotnet Cube, where you will learn and get solutions about programming, web designing and development. I started this website to share my knowledge and experience which might help someone.

You can get more details about me and my blog at About Us page.

Subscribe Newsletter

Subscribe to our monthly newsletter to get the latest industry news, Media Group updates and our current specials.

Follow On Us...

Home | About | Advertise | Copyrights | Privacy | Contact | Admin

Copyrights © 2015-2019 dotnetcube.com. All rights reserved