-3

When should we actually use ref and out. I know the difference between the two. Before you can pass a ref parameter, you must assign it to a value. which is not compulsory in out.

But when should we use ref. ??

I read this, but didnt got, when should I use ref.

http://www.dotnetperls.com/ref

user2163048
  • 23
  • 1
  • 7

1 Answers1

0

Here is an example:

    static void Main(string[] args)
    {

        int i = 1;

        foo(i);
        Console.Write(i); //i=1;

        Reffoo(ref i);
        Console.Write(i); //i=2;
    }

    static void Reffoo(ref int i)
    {
        i++;
    }

    static void foo(int i)
    {
        i++;
    }
jacob aloysious
  • 2,547
  • 15
  • 16