Introduction
In C Sharp (C#) we can have three types of parameters in a function. The parameters can be In parameter (which is not returned back to the caller of the function), out parameter and ref parameter (where by a reference to the variable is ed back).
The first thing that we will take a look at, is the out and ref modifiers. C#, and other languages as well, differ between two parameters: "by value" and "by reference". The default in C# is "by value", which basically means that when you on a variable to a function call, you are actually sending a copy of the object, instead of a reference to it. This also means that you can make changes to the parameter from inside the function, without affecting the original object you ed as a parameter.
Consider the following example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace simplevalue
{
class Progra
{
static void Addvalue(int val)
{
val++
}
static void Main(string[] args)
{
int value = 10;
Addvalue(value);
Console.WriteLine(value);
Console.Read();
}
}
}
Output: 10
We create an integer, assign the number 10 to it, and then we use the Addvalue() method, which should add 1 to the number. But does it? No. The value we assign to number inside the function, is never carried out of the function, because we have ed a copy of the number value instead of a reference to it. This is simply how C# works, and in a lot of cases, it's the preferred result. However, in this case, we actually wish to modify the number inside our function. Enter the ref and out keyword:
The out and ref keyword are looks quite similar in nature. Both parameters are used to return back some value to the caller of the function. But still there is a small but important difference between them. Both of the parameter type has been kept in the C# language for specific scenario.
"out" keyword
The out parameter can be used to return the values in the same variable ed as a parameter of the method. Any changes made to the parameter will be reflected in the variable.
The out keyword causes arguments to be ed by reference. This is similar to the refkeyword, except that ref requires that the variable be initialized before being ed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.
For example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace outexample
{
class Program
{
static void value(out int val)
{
val = 20;
}
static void Main(string[] args)
{
int value;
value(out value);
Console.WriteLine(value);
Console.Read();
}
}
}
Output: 20
When we use the out parameter, The program calling the function need not assign a value to the out parameter before making the call to the function. The value of the out parameter has to be set by the function before returning the value.

Yogesh MhadgutPosted Jul 15, 2019, 4:14 AM
What is mean of ed
Upendra Pratap ShahiPosted Jun 5, 2015, 2:28 AM
According to me there are four type parameter-1-Value 2-params 3-ref 4-out
Upendra Pratap ShahiPosted Jun 5, 2015, 2:12 AM
very nice article...
sai nadhPosted Feb 26, 2015, 5:23 AM
its simple and good for beginners
Manju lata YadavPosted Jul 4, 2014, 2:06 AM
This article is focussing on why to use ref and out. Its good.