Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#
Tip/Trick

C# 7 - New Inline Out Variables

Rate me:
Please Sign up or sign in to vote.
4.68/5 (4 votes)
2 Oct 2017CPOL1 min read 35.3K   3
C# 7 has the ability to declare a variable right at the point where it is passed as an out argument

Introduction

In C# prior to version 7, to use an out variable, you must first declare a variable of the correct type, before actually using it, normally on the next line of code, as an out parameter, like:

C#
var input = DateTime.Now.ToShortDateString();

DateTime start;
if (DateTime.TryParse(input, out start))
{
    // Do something with start.
}

This is really only trivially inconvenient, but the separation between the 'start' variable and the following code doesn't quite smell all roses.

Background

In C# 7, we now have what they call Inline Out Variables. In plain English, we can inline the declaration of an out variable in the same place it is used. This is a more cohesive approach without reliance on any external variable.

Using the Code

To use the out variables in the purer way, that is typed, you insert the type of the out variable immediately to the left of the variable name. This should place the type name right in front of the out keyword:

C#
if (DateTime.TryParse(input, out DateTime start))
{
    // Do something with start.
}

We can also use implicitly typed variables as inline out variables:

C#
if (DateTime.TryParse(input, out var start))
{
    // Do something with start.
}

Points of Interest

C# 7 is chock full of little but over some time, very, very handy improvements and even just shortcuts. I suggest if this interested you, have a look at the two artefacts: ref locals and returns. I'm quite certain I may even publish a tip here with them quite soon.

History

First time!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Erisia Web Development
South Africa South Africa
I am a software developer in Johannesburg, South Africa. I specialise in C# and ASP.NET MVC, with SQL Server, with special fondness for MVC and jQuery. I have been in this business for about eighteen years, and am currently trying to master Angular 4 and .NET Core, and somehow find a way to strengthen my creative faculties.
- Follow me on Twitter at @bradykelly

Comments and Discussions

 
SuggestionDon't use out variables anymore when possible Pin
DavesApps3-Oct-17 2:28
DavesApps3-Oct-17 2:28 
GeneralRe: Don't use out variables anymore when possible Pin
John Brett9-Oct-17 5:59
John Brett9-Oct-17 5:59 
QuestionA small addition perhaps Pin
Wendelius2-Oct-17 17:33
mentorWendelius2-Oct-17 17:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.