如何显式丢弃out参数? [英] How to explicitly discard an out argument?

查看:83
本文介绍了如何显式丢弃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}");
}

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

这篇关于如何显式丢弃out参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆