Using the ref Parameter


The ref keyword on a method parameter causes a method to refer to the same variable that was passed as an input parameter for the same method. If you do any changes to the variable, they will be reflected in the variable.

You can even use ref for more than one method parameters.

namespace TestRefP
{
using System;
public class myClass
{
public static void RefTest(ref int iVal1 )
{
iVal1 += 2;
}
public static void Main()
{
int i; // variable need not be initialized
i = 3;
RefTest(
ref i );
Console.WriteLine(i);
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all