Skip to main content
Out variables in CSharp 7.0 with example

Out variables in CSharp 7.0 with example

In C#, a method can return only one value. If you would like to return more than one value, you can use the out keyword.

Currently in C#, using out parameters isn’t as fluid as we would like. Before you can call a method with out parameters, you first have to declare variables to pass to it. Since you typically aren’t initializing these variables, they are going to be overwritten by the method after all. You need to specify the full type and cannot just use var to declare them.

public void PrintCoordinates(Point p)
{
    int x, y; //pre-declare out variables before using them
    p.GetCoordinates(out x, out y);
    Console.WriteLine($"({x}, {y})");
}

In C# 7.0, some enhancements have been done for out variables. Now you have the ability to declare a variable right at the point where it is passed as an out argument as shown in below code snippet.

public void PrintCoordinates(Point p)
{
   p.GetCoordinates(out int x, out int y);
   Console.WriteLine($"({x}, {y})");
}

To know more about C# features with respect to its versions, navigate to the below post.

List of C# features version by version

Out variables in C# 7.0 #csharp #csharp7.0 #microsoft #developers Click To Tweet

Notice that the variables are in scope in the enclosing block, so the subsequent line can use them. Most kinds of statements do not establish their own scope, so out variables declared in them are usually introduced into the enclosing scope.

Note: In Preview 4, the scope rules are more restrictive.

Which means, out variables are scoped to the statement they are declared in. Thus, the above example will not work until a later release.

Since the out variables are declared directly as arguments to out parameters, the compiler can usually tell what their type should be (unless there are conflicting overloads), so it is fine to use var instead of a type to declare them:

p.GetCoordinates(out var x, out var y);

A common use of out parameters is the Try… pattern, where a Boolean return value indicates success, and out parameters carry the results obtained: For an instance, a web service call.

public void DisplayDollars(string s)
{
   if (int.TryParse(s, out var i)) 
       Console.WriteLine(new string('$', i)); 
   else
       Console.WriteLine("You don't owe any dollars to us!");             
}

Note: Here i is only used within the if-statement that defines it, so Preview 4 handles this fine.

The planned features list of C#7.0 is to allow “wildcards” as out parameters as well, in the form of a *, to let us ignore out parameters which we don’t care about:

p.GetCoordinates(out int x, out *); // We only care about x

Note: It is still not clear whether wildcards make it into C# 7.0.

What do you think?

Dear Readers,
If you have any questions or suggestions please feel free to email us or put your thoughts as comments below. We would love to hear from you. If you found this post or article useful then please share along with your friends and help them to learn.

Happy Programming!

Was this article worth reading? Share it with fellow developers too.

DotNetCrunch

DotNetCrunch is a popular blog for latest Microsoft® technologies which is aimed for beginners and intermediate level professionals.

25