Thursday, August 17, 2017

Ref keyword in c#

Ref is a c# keyword use to for pass argument by reference.So any change in the function or method it will reflect in the variable when it called.

Example:

public static string Gamevalue(ref int id)
{
    string Text = "Next-" + id.ToString();
    id += 2;
    return Text;
}
static void Main(string[] args)
{
    int i = 1;
    Console.WriteLine("Previous value :" + i.ToString());
    string test = Getvalue(ref i);
    Console.WriteLine("Current value:" + i.ToString());
}

output:
Previous value : 1
Current value  :  3


I wish you are happy to use the Ref keyword while you programming on c#. If anything to know about programming language c# please comment. I will make a lesson for you.  

No comments:

Post a Comment