The Wayback Machine - https://web.archive.org/web/20240118173404/https://www.geeksforgeeks.org/difference-between-ref-and-out-keywords-in-c-sharp/
Open In App
Related Articles

Difference between Ref and Out keywords in C#

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values. The out parameter does not pass the property.

Example :




// C# program to illustrate the
// concept of out parameter
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Declaring variable
        // without assigning value
        int G;
  
        // Pass variable G to the method
        // using out keyword
        Sum(out G);
  
        // Display the value G
        Console.WriteLine("The sum of" + 
                " the value is: {0}", G);
    }
  
    // Method in which out parameter is passed
    // and this method returns the value of
    // the passed parameter
    public static void Sum(out int G)
    {
        G = 80;
        G += G;
    }
}


Output:

The sum of the value is: 160

The ref is a keyword in C# which is used for the passing the arguments by a reference. Or we can say that if any changes made in this argument in the method will reflect in that variable when the control return to the calling method. The ref parameter does not pass the property.

Example:




// C# program to illustrate the
// concept of ref parameter
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Assign string value
        string str = "Geek";
  
        // Pass as a reference parameter
        SetValue(ref str);
  
        // Display the given string
        Console.WriteLine(str);
    }
  
    static void SetValue(ref string str1)
    {
  
        // Check parameter value
        if (str1 == "Geek") {
            Console.WriteLine("Hello!!Geek");
        }
  
        // Assign the new value
        // of the parameter
        str1 = "GeeksforGeeks";
    }
}


Output:

Hello!!Geek
GeeksforGeeks
Difference between Ref and Out keywords
ref keyword out keyword
It is necessary the parameters should initialize before it pass to ref. It is not necessary to initialize parameters before it pass to out.
It is not necessary to initialize the value of a parameter before returning to the calling method. It is necessary to initialize the value of a parameter before returning to the calling method.
The passing of value through ref parameter is useful when the called method also need to change the value of passed parameter. The declaring of parameter through out parameter is useful when a method return multiple values.
When ref keyword is used the data may pass in bi-directional. When out keyword is used the data only passed in unidirectional.

Note: Both ref and out parameter treated same at compile-time but different at run-time.


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 23 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads