How to explicitly discard an out argument?

后端 未结 8 1050
情歌与酒
情歌与酒 2020-12-09 14:30

I\'m making a call:

myResult = MakeMyCall(inputParams, out messages);

but I don\'t actually care about the messages. If it was an input pa

8条回答
  •  天涯浪人
    2020-12-09 15:07

    Starting with C# 7.0, it is possible to avoid predeclaring out parameters as well as ignoring them.

    public void PrintCoordinates(Point p)
    {
        p.GetCoordinates(out int x, out int y);
        WriteLine($"({x}, {y})");
    }
    
    public void PrintXCoordinate(Point p)
    {
        p.GetCoordinates(out int x, out _); // I only care about x
        WriteLine($"{x}");
    }
    

    Source: https://blogs.msdn.microsoft.com/dotnet/2017/03/09/new-features-in-c-7-0/

提交回复
热议问题