Friday, 5 February 2016

How to passed value type by reference

using System;

class RefType
{
    public void sqr(ref int i)
    {
        i = i * i;
    }
}

class MainClass
{
    public static void Main()
    {
        RefType ob = new RefType();

        int x = 20;

        Console.WriteLine("a before call: " + x);

        ob.sqr(ref x);

        Console.WriteLine("a after call: " + x);
    }
}
------------------------
OUTPUT:-
a before call: 20
a after call: 400
----------------------------------------------------

No comments:

Post a Comment