Duplicate of: What is the purpose of the “out” keyword at the caller?
Why I need to use 'ref' keyword in both declaration and Call.
void foo(ref int i)
{
}
For example, Consider above function. If I will call it without ref keyword as
foo(k);
it will give me error :
Argument '1' must be passed with the 'ref' keyword
Why isn't it enough to just specify in the method signature only ?
This is because ref
indicates that the parameter should be passed in by reference. It is something like pointer in C++
For example;
void CalFoo()
{
var i=10;
foo(ref i); //i=11
}
void foo(ref int i)
{
i++;
}
but
void CalFoo()
{
var i=10;
foo(i); //i=10
}
void foo(int i)
{
i++;
}
I think you can have both foo(ref int)
and foo(int)
in one single class. So if you don't specify the ref
.. how does the compiler knows which one to call?