C# Interview Questions and Answers
<< Previous Question Next Question >>
 Question: 2738 Page Views: 

What is "out" keyword in C#?



Posted By: Avi Date: 5 December 2009 12:09:31 AM
 Answer:

The "out" keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed.

It is useful when we want to return more than one value from the method.

To use an out parameter, both the method definition and the calling method must explicitly use the out keyword and the value to "out" parameter must be assigned in the method body, otherwise the method will nor be compiled.

Sample code for "out" keyword:

class Test
{
public static void Main()
{
int i;
TestMethod(out i);
Console.WriteLine("The value of i is: " + i);
}

public static void TestMethod(out int j)
{
j=10;
}
}

The program will result:
The value of i is: 10


Posted By: eTechPlanet


Date: 5 December 2009 12:09:31 AM

“out Keyword” in C#.out keyword is used for passing a variable for output purpose. It has same concept as ref keyword, but passing a ref parameter needs variable to be initialized while out parameter is passed without initialized.
It is useful when we want to return more than one value from the method.

Point to be kept in mind while using out keyword.
You must assigned value to out parameter in method body, otherwise the method won’t compiled.

Example of “out keyword”:
class Test
{
public static void Main()
{
int a; // may be left un-initialized
DoWork(out a); // note out
Console.WriteLine("The value of a is " + a);
}

public static void DoWork(out int i) // note out
{
i=4; //must assigned value.
}
}

The program will result : The value of a is 4


Posted By: Johndecruse


Date: 31 January 2010 10:35:01 PM
Post a better Answer if you have
 
(Will show your Gravatar icon)  
  Country flag

Loading
Enter the text as shown in the image 8nE7XZ
Related Questions
C# : How do you use random number generator in c#?

How do you use random number generator in c#?

Random r = new Random(); double t = r.NextDouble();
Category: C# Date: 10/14/2010 1:12:47 AM
C# : What would be the type of variable x? 
var x = new int[] {4,5,6};

What would be the type of variable x? var x = new int[] {4,5,6};

System.Int32[]
Category: C# Date: 10/14/2010 12:20:46 AM
C# : How do you declare extension method for string class?

How do you declare extension method for string class?

static class Sample { public static Function(this string s, /* other parameters */) {} }
Category: C# Date: 10/14/2010 12:03:31 AM
C# : What will be result of this code:
class C1 
{ protected int A = 0;
  public void B() { console.writeLine(A); }}
class C2 : C1 {public void B() { Console.WriteLine(A+10); }}
static void Main(string[] args)
{
C1 c = new C2();
c.B();
}
C# : What does the #region directive do?

What does the #region directive do?

It defines region in the code that can be collapsed into one line in Visual Studio
Category: C# Date: 10/13/2010 11:09:14 PM