Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

place the "out type " declaration while calling the method

Tags:

c#

.net

I am trying to use the 'out parameter' within a .NET 4.5.2 application, but I am getting a compile error.

Questions: In which framework can I compile this? What does this called? Inline variable declaration in out method calling?

Could you please provide a reference?

Ref: https://www.dotnetperls.com/parse

new out syntax: We can place the "out int" keywords directly inside a method call. Older versions of C# do not allow this syntax. But this can reduce the line count of a program.

static void Main()
{
    const string value = "345";
    // We can place the "out int" declaration in the method call.
    if (int.TryParse(value, out int result))
    {
        Console.WriteLine(result + 1);
    }
}
like image 279
Daniel B Avatar asked Nov 02 '25 10:11

Daniel B


1 Answers

This requires C# version 7 that ships with Visual Studio 2017.

The feature is called "Out Variables".

In C# 7.0 we have added out variables; the ability to declare a variable right at the point where it is passed as an out argument:

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

Further documentation can be found here.

like image 132
Bradley Uffner Avatar answered Nov 03 '25 23:11

Bradley Uffner