Hi................
I am not understand what is a used of "ref" and "out" keywords of c# ?
please expalin and give a example.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Datta KharadPosted Nov 28, 2011, 1:55 AM
Ref and Out:-
Out is used whenever you want to expect only output value and doesn't send input. Ref is used whenever you want to expect output value according to input i,e send input and get output using single variable. Ref parameters allow you to pass variable reference parameters, allowing you to assign the parameter in the called method and have it also be assigned at the calling site. This is powerful and useful but also has disadvantages. Here we look at how you can use the ref and out keywords on parameters. A variable passed with ref must be assigned first. One with out must be assigned in the called method.
Example
There is a big difference between ref and out parameters at the language level. Before you can pass a ref parameter, you must assign it to a value. This is for the purpose of definite assignment analysis. On the other hand, you don't need to assign an out parameter before passing it to a method. However, the out parameter in that method must assign the parameter before returning.
using System;
class Program
{
static void Main()
{
string value1 = "book"; // Assign string value
SetString1(ref value1); // Pass as reference parameter
Console.WriteLine(value1); // Write result
string value2; // Unassigned string
SetString2(1, out value2); // Pass as out parameter
Console.WriteLine(value2); // Write result
}
static void SetString1(ref string value)
{
if (value == "book") // Test parameter value
{
Console.WriteLine("Is book");
}
value = "notebook"; // Assign parameter to new value
}
static void SetString2(int number, out string value)
{
if (number == 1) // Check int parameter
{
value = "one"; // Assign out parameter
}
else
{
value = "pen"; // Assign out parameter
}
}
}
//Output
Is book
notebook
one
Description. The program in the example defines the Program class with the Main entry point and two static methods, SetString1 and SetString2. The methods have formal parameter lists with the reference parameter keywords.
SetString1 method with ref string. The SetString1 method shown above has the "ref" keyword in its method signature. This requires that whenever you pass a string to this method, it must be described with the "ref" keyword before the identifier. When the method is called, the storage location of the string variable is copied to the method. None of the characters pointed to by the string variable are copied.
SetString2 method with out string. The SetString2 method has the "out" keyword in its signature. This means that whenever you want to call it, you must describe the string parameter with the "out" keyword. You do not need to assign the string parameter before sending it to SetString2. However, you will get a compile-time error if you do not assign the string parameter before any return points.
Posted Nov 27, 2011, 8:56 AM
When you use a reference parameter, the passed variable must have an assigned value.
Reference Parameter
using System;
public class ParameterDemo2
{
public static void Main()
{
int var = 4;
Console.WriteLine("In Main var is {0}", var);
MethodWithRefParam(ref var);
Console.WriteLine("In Main var is {0}", var);
}
public static void MethodWithRefParam(ref int parm)
{
parm = 888;
Console.WriteLine("In MethodWithRefParam, param is {0}", parm);
}
}
/*
In Main var is 4
In MethodWithRefParam, param is 888
In Main var is 888 (Notice value has been alered 4 to 888)
*/
Using an output parameter is convenient when the passed variable doesn't have a value yet. For example, the below program uses InputMethod() to obtain values for two parameters. The parameters get their values from the method, so it makes sense to provide them with no values going in, but to have them retain values coming out
using System;
public class InputMethodDemo
{
public static void Main()
{
int first, second;
InputMethod(out first, out second);
Console.WriteLine("After InputMethod first is {0} and second is {1}", first, second);
}
public static void InputMethod(out int one, out int two)
{
string s1, s2;
Console.Write("Enter first integer ");
s1 = Console.ReadLine();
one = Convert.ToInt32(s1);
Console.Write("Enter second integer ");
s2 = Console.ReadLine();
two = Convert.ToInt32(s2);
}
}
/*
Enter first integer 2
Enter second integer 4
After InputMethod first is 2 and second is 4
*/
VulpesPosted Nov 27, 2011, 6:57 AM
Hemant KumarPosted Nov 27, 2011, 6:55 AM
Ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.
So while ref is two-ways, out is out-only.