C# language: ref, out i params modifiers parameters

in #programming6 years ago

CC.png

I do not know if you were wondering, but the variables sent as parameters to the method are copies because the method will not affect the sent variable and you may wonder if you can do so that the variable can be changed in the method to which we sent it, the modifiers ref and out are serve to that about which will be discuss today.

Ref Modifier

I will use a trivial example to make it easy to understand, first a standard example of sending a variable as an argument to the method:

class Program
{
    static void Main(string[] args)
    {
        int a = 12;
        ChangeSendArgument(a);
 
        Console.WriteLine(a);
 
        Console.ReadKey();
    }
 
    static void ChangeSendArgument(int i)
    {
        i += 1;
        Console.WriteLine(i);
    }
}

It is known that the console will first show the number changed in the ChangeSendArgument method, that is 13, and then the number 12 so variable that we sent as an argument, ie the variable “a” has not changed, and we want the variable “a” sent as an argument, if will change in the method to which we sent this variable to change also in the place where we sent it. How to do it, very simply, just add two keywords:

static void Main(string[] args)
{
    int a = 12;
    ChangeSendArgument(ref a); // we command ChangeSendArgument to work directly with a
 
    Console.WriteLine(a); // a has value 13 now
 
    Console.ReadKey();
}
 
static void ChangeSendArgument(ref int i)
{
    i += 1;
    Console.WriteLine(i);
}

I think that this example with comments is clearly enough to explain this, increasing the argument in the ChangeSendArgument method by one also changes the variable “a” by one, that is, you can say that they cooperate with each other 🙂

Out Modifier

The modifier out differs from the ref that:

  1. It do not need to have a value assigned before it is passed to a function.

  2. Must have a value assigned before leaving the function.

Below is an example:

static void Main(string[] args)
{
    int a; //no value
    ChangeSendArgument(out a);
 
    Console.WriteLine(a);
 
    Console.ReadKey();
}
 
static void ChangeSendArgument(out int i)
{
    i = 5; // It must have a value before it exits the function
    Console.WriteLine(i);
}

As in the points above, the difference is that in the beginning there is no value.

Params Modifier

The params modifier means that the method can take any number of arguments. Below is an example:

static void Main(string[] args)
{
     ChangeSendArgument(23,45,26,78);
 
     Console.ReadKey();
}
 
static void ChangeSendArgument(params int[] i)
{
   for (int a = 0; a < i.Length; a++)
   {
      Console.WriteLine(i[a]);
   }
}

This content also you can find on my blog http://devman.pl/csharplan/ref-i-params-modifiers-parameters/

If you recognise it as useful, share it with others so that others can also use it.

Leave upvote and follow and wait for next articles :) .

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.027
BTC 60063.85
ETH 2313.06
USDT 1.00
SBD 2.46