如何显式丢弃out参数?

编程入门 行业动态 更新时间:2024-10-09 11:19:01
本文介绍了如何显式丢弃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参数?

本文发布于:2023-11-23 13:20:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!