out keyword in c#

You can use the keyword out in c# to pass the arguments by reference .

The out keyword works almost the same as ref keyword but one of the difference between the out and ref keyword is that out doesn’t require the variable to be initialized where as the ref keyword requires that the variable be initialized before passing it to the function.

Below is a simple example that demonstrates the out keyword .

class Student
{
        public string StudentName { get; set; }
        /* usage of the pass by reference using the keyword ref */
        public void Add(out int i)
        {
            i = 0;
            i = i + i;
        } 
}
class Program
{
        public static void Main()
        {
            Student studentObj = new Student();        
            Console.WriteLine("i = : " + i);
            int i;
            studentObj.Add(out i); 

            Console.WriteLine("i: " + i);
        }
}
%d bloggers like this: