Friday, 5 February 2016

How to passed Objects by reference

using System;

class Test
{
    public int m, n;

    public Test(int i, int j)
    {
        m = i;
        n = j;
    }

    public void change(Test ob)
    {
        ob.m = ob.m + ob.n;
        ob.n = -ob.n;
    }
}

class MainClass
{
    public static void Main()
    {
        Test ob = new Test(10, 30);

        Console.WriteLine("ob.a and ob.b before call: " +
                           ob.m + " " + ob.n);

        ob.change(ob);

        Console.WriteLine("ob.a and ob.b after call: " +
                           ob.m + " " + ob.n);
    }
}
-----------------------------------
OUTPUT:-

ob.a and ob.b before call: 10 30
ob.a and ob.b after call: 40 -30
---------------------------------------------

No comments:

Post a Comment