Keyword out and code examples in C#

Reading Time: 1 minute

Both keyword ref and keyword out in C# change the behavior of called method parameters. The difference is that the parameter passed as ref must be initialized before passing to the method. Parameter passed with keyword out in C# needs not to be initialized before it passed. So it is practical to use keyword out in C# in order to check if entered data is number, for example:

public string strA;
public int A;

bool isNumericA = int.TryParse(strA, out A);
if (isNumericA) { }
else { }

Another example is to use keyword out to define values of several parameters:

static void value(out int value1, out int valvalue2, out string s1)

value1 = 20;

value2 = 40;

s1 = “Hello!”;

static void Main(string[] args)

int val1, val2;

string str_s1;

value(out val1, out val2, out str_s1);

Console.WriteLine({0}, val1);



External links:

Keyword out in C# code example on Dotnetperls
Keyword out in C# code example on Stackoverflow

Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *