ref and out keyword in c#

Out keyword in C#

In Out key, When we passed variables as out arguments that time we initialized to the variables within the methods. It is also known as output parameter.

Ref keyword is a one-way communication while the out keyword is a two-ways communication.
  1. Initialized must be inside the methods. 
  2. Before calling the methods may be initialized or not initialized the values of the variables.
A simple code sample as given below


public class OutClass
{
    static void Method(out int i)
    {
        i = 10;
    }
    static void Main()
    {
        int value;
        Method(out value);
    }
}

Some important notes as given below

  1. The ref and out keywords are treated differently at run-time but they are treated the same at compile time. 
  2. Methods cannot be overloaded if one method takes a ref keyword and the other takes an out keyword.

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.