本文介绍了如何显式丢弃out参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在拨打电话:
myResult = MakeMyCall(inputParams, out messages);但我实际上并不关心消息。如果它是输入参数,我不在乎,我只是传入一个null。如果这是回报,我不在乎,就别管它了。
but I don't actually care about the messages. If it was an input parameter I didn't care about I'd just pass in a null. If it was the return I didn't care about I'd just leave it off.
是否可以用out做类似的事情,还是需要声明一个变量,然后将其忽略?
Is there a way to do something similar with an out, or do I need to declare a variable that I will then ignore?
推荐答案从C#7.0开始,可以避免预先声明参数并忽略它们。
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}"); }来源: blogs.msdn.microsoft/dotnet/2017/03/09/ new-features-in-c-7-0 /
更多推荐
如何显式丢弃out参数?
发布评论