C# | C Sharp Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the reference causes the permanent modification in the original value . Let's see a simple example of passing reference in a function. 

using System;
public class Program
{
            public int square (ref int m)
            {
                        return m*m;
            }
            public static void Main(string[] args)
            {
                        int num=Convert.ToInt32(Console.ReadLine());
                        Program p1=new Program();
                        int m=p1.square(ref num);
                        Console.WriteLine(m);                     
            }
}
Output
100
100 X 100 is 10000



ADVERTISEMENT
ADVERTISEMENT