I am new to C#. I've tried this with out parameter in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class First
{
public void fun(out int m)
{
m *= 10;
Console.WriteLine("value of m = " + m);
}
}
class Program
{
static void Main(string[] args)
{
First f = new First();
int x = 30;
f.fun(out x);
}
}
but i get some errors like "Use of unassigned out parameter 'm'" and
The out parameter 'm' must be assigned to before control leaves the current method.
So what is the meaning of these errors and why it is compulsory to assign 'm' when i'm already assigned a value to x.
out
parameters withref
parameters.fun()
does not have access to the previous value of whateverm
is pointing at, and thus can't multiply it. This distinction is intentional,out
parameters are meant to let you have multiple return values. It's as if you were trying to create a function that multiplies the value of the variable its result is being assigned to. – Epicontinental