Pass by ref for object parameters : Object Instance « Class Interface « C# / C Sharp






Pass by ref for object parameters

 


using System;
using System.Collections.Generic;
using System.Text;

class Pass {
    public static void Value(ref int param) {
        param = 42;
    }

    public static void Reference(WrappedInt param) {
        param.Number = 42;
    }
}
class WrappedInt {
    public int Number;
}

class Program {
    static void Entrance() {
        int i = 0;
        Console.WriteLine(i);
        Pass.Value(ref i);
        Console.WriteLine(i);

        WrappedInt wi = new WrappedInt();
        Console.WriteLine(wi.Number);
        Pass.Reference(wi);
        Console.WriteLine(wi.Number);
    }

    static void Main(string[] args) {
        try {
            Entrance();
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}

 








Related examples in the same category

1.Object.ReferenceEquals Method
2.Calling an Instance Method on an Object
3.Calling a Class Method on a Class
4.Catch NullReferenceException Exception
5.Object assignment
6.Point object