Ref and Out keyword in C#

Ref and Out Keyword in C#

Before going to learn about ref and out keyword the pre-requisite is we required to know what is value type and reference type, so let start with Reference type :-

Reference type are pointer type which store an memory address and point to a memory location; and store on a heap memory. Hear key point is reference type are uses both stack and heap that means it store the address inside heap and the value are stored inside stack,so it means without stack their is no heap at all.

Example : All user defined  classes.
Value types are those which directly store value inside it, and it uses stack memory.
Example: All pre-defined data types. (Ex: int,float, double & etc)
KeyCapsule: for example I do like this:
  1. int x=4;
  2. int x1=x;
  3. then again if x=100;

And if we print x1 the output we get is 4 only, So it cannot be changed because it holds the value directly but in case of reference type changes in one type must reflect to another type.

Now let’s start with our main topic Ref and Out Keyword :

Ref Keyword:

Ref keyword can be used with any data type. The main purpose of ref keyword is to make any type to reference type i.e the when we add ref keyword at method calling time the c# compiler does not create a new variable it just create copy of that type which only point to that particular variable which we declared and make any changes to that variable it will reflect on that variable for ex: we do like that:

  1. int x=10;
  2. show(ref x)
  3. //Method declaration

Public static show (ref int number) //Hear it not created a new variable rather then it make a copy of that type so made any changes to that reflect to original variable, that’s how ref used in primitive data type.

For example:

  1. class RefDemo
  2. {
  3.    public static void Show(int number)
  4.    {
  5.       number= 23;
  6.    }
  7.    static void Main()
  8.    {
  9.       int x=10;
  10.       Show(x);
  11.       Console.WriteLine(x);
  12.    }
  13. }

 

In the above output is 10 , because all value type variable are directly storing value so due to this hear if we print number we get output as 23 and for x we got 10.

But it get changes when we call this method through ref keyword which can be as following :

  1. class RefDemo
  2. {
  3.    public static void Show(ref int number)
  4.    {
  5.       number= 23;
  6.    }
  7.    static void Main()
  8.    {
  9.       int x=10;
  10.       Show(ref x);
  11.       Console.WriteLine(x);
  12.    }
  13. }

Hear we got the output as 23 because c sharp compiler don’t create variable it make a copy of type x. So changes made any where reflect to x.

In class or all reference type variable it also use the same mechanism. As we know that in case of Class type without using ref also they point to same object when we assign one object to class variable. Consider Case 1.

Case 1:

  1. class RefDemo
  2. {
  3.    public static void Show(GradeData data) //GradeData is class
  4.    {
  5.       data.name= “SURYA”;
  6.    }
  7.    static void Main()
  8.    {
  9.       GradeData obj=new GradeData();
  10.       GradeData obj1;
  11.       obj1.name=“Rajani”;
  12.       obj1=obj;
  13.       Show(obj1)
  14.       Console.WriteLine(obj1.name);
  15.    }
  16. }

Hear we got output as “SURYA”.

Consider second case.

Case 2:

  1. class RefDemo
  2. {
  3.    public static void Show(GradeData data) //GradeData is class
  4.    {
  5.       GradeData data=new GradeData();
  6.       data.name= “SURYA”;
  7.    }
  8.    static void Main()
  9.    {
  10.       GradeData obj=new GradeData();
  11.       GradeData obj1;
  12.       obj1.name=“Rajani”;
  13.       obj1=obj;
  14.       Show(obj1)
  15.       Console.WriteLine(obj1.name);
  16.    }
  17. }

Hear the output is “Rajani” not “SURYA” because although we sending obj1 as parameter we can not get “SURYA” as output because data is now new instance but in first case it only variable.
so to make that instance also point to same variable we use ref keyword.

For example:

  1. class RefDemo
  2. {
  3.    public static void Show(ref GradeData data) //GradeData is class
  4.    {
  5.       GradeData data=new GradeData();
  6.       data.name= “SURYA”;
  7.    }
  8.    static void Main()
  9.    {
  10.       GradeData obj=new GradeData();
  11.       GradeData obj1;
  12.       obj1.name=“Rajani”;
  13.       obj1=obj;
  14.       Show(ref obj1)
  15.       Console.WriteLine(obj1.name);
  16.    }
  17. }

Hear we got the output as “SURYA”.

Because due to use of ref keyword c# compiler treat the data object as copy of obj1 type so any changes made to data reflect on obj1/obj.

Key Capsule

Before we use ref keyword at calling time of method, it must be initialized whether it is value type or ref type.

Out keyword:

The out is also used for same purpose and we can use it for both value type and reference type but the main difference is that:

Before calling the method in using ref keyword we must assign the variable which used in that method where as in case of Out we do n’t required to assign the value before calling the method it means c# compiler do n’t borther about that whether you assigned or not but it is mandatory when the controller goes to particular method, in that method we must require to initialize the variable before the control out of that method.

Another difference is that when out used with the value type then we can return more than one value from a method at a time.

For example:

  1. class OutDemo
  2. {
  3.    public static void GetData(int c, out int a, out int b)
  4.    {
  5.       a = c + 5;
  6.       b = c + a;
  7.    }
  8.    static void Main()
  9.    {
  10.       int a,b;
  11.       GetData(5, out a, out b);
  12.       Console.WriteLine(a + “\n” + b);
  13.       Console.ReadLine();
  14.    }
  15. }

Hear it will print a=10 and b=15.

Last but not the list

Important Key Capsule

Remember that while compilation both out and ref keyword are not taken consider by the CSC compiler it only checked by standard language infrastructure, so if you are writing like this:

  1. public static void Getdata(ref int a)
  2. {
  3. }
  4. public static void Getdata(out int a)
  5. {
  6. }

And thinking that it is an overloaded method then you are wrong both are same only. Hear C# compiler do not take consider to ref and out as parameter so it shows you error at the runtime.Hence Ref and out playing an important role in application development.

I think above all might gave you an clear picture about ref and out.

Thanks for reading hope this useful for you.

Please replay me comment is there anything wrong or give me suggestion. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s