Out and Ref are C# keywords help to pass variables by reference.
In the ref no need to initialize variable in function definition. Ref is Two-way from caller to callee.
static void SomeFunction(ref int value)
{
value = value + 10;
}
int value = 20;
SomeFunction(ref value);
Console.WriteLine(value);
Here out is 30.
In the Out we need to initialize variable in function definition.
Data sent from the caller has been discarded and its mandatory to initialize the variable inside the callee.
"Out is One-Way from caller to callee"
static void SomeFunction(out int value)
{
value = 0; // In out we need to initialize the variable value
value = value + 10;
}
int value = 20;
SomeFunction(out value);
Console.WriteLine(value);
Here output is 10.
No comments:
Post a Comment