Refinance now before rates go up! Get multiple rate quotes at GetMyLender.com.

What is the Difference between Ref and Out in c sharp with Examples

In this post we will learn about ref and out parameters in c# with examples. While looking forward, we have to know about ref and out parameters and how we can use it and where to use ? Both ref and out parameters are used to pass as an arguments to a method. Actually method can return zero or one value, so these parameters are very helpful whenever methods want to return more than one value.

So, what is the main difference between these both parameters?

Both parameters used for same purpose but ref parameter need to initialize it before passing to the method and out parameter you don’t need to initialize before passing to function. Here we will go in more detail by using examples.


Ref Parameter

Here meaning of ref is Reference. Before passing ref parameter/variable to the method, we need to initialize it. Ref keyword will pass as a reference parameter this means when the value of parameter is changed in called method it will reflected in calling method also.


                int x; // variable declaration
                x = 5; // variable need to be initialized
                ReferenceMethod(ref x); 
        

If you observe above code first we declared variable and initialized with value 5 before it pass a ref parameter to ReferenceMethod () method.

Example

                class Program 
                { 
                    Public static void Main()
                    { 
                        int x;
                        x = 5;
                        ReferenceMethod (ref x);
                        Console.WriteLine(“Value of X is ” + x); 
                    }
                    
                    public static void ReferenceMethod (ref int y)
                    {
                        y += 3;
                    }
                }
        

When we run above code we will get Output :
Value of X is 8

Out Parameter

Unlike ref parameter we don’t need to initialize out parameter before passing it as out parameter to method.


                int x; // No need to initialize variable 
                OutMethod(out x);
        

If you observe above code first we declared variable and we it pass x out parameter to OutMethod () method without initializing.

Example

                class Program 
                { 
                    Public static void Main() 
                    { 
                        int x; 
                        Outsample(out x); 
                        Console.WriteLine(“Value of X is ” + x); 
                    } 
                    
                    public static void OutMethod (out int y) 
                    { 
                        y = 5; 
                    } 
                }
        

When we run above code we will get Output :
Value of X is 5

Conclusion :

I hope that this article would have helped you in understanding difference between ref and out parameters in c#. Please share your knowledge if you know more about this attribute. Your feedback and constructive contributions are always welcome.


No comments:

Post a Comment