有没有办法省略参数?
假设我有一个带有 out
参数的函数,但我不需要它的值。如果给定的结果无论如何都会被丢弃,是否有办法不传递实际参数?
编辑:
尽管该问题已被投票为“可选输出参数”的欺骗 只有从方法创建者的角度来看才会如此。如果您是该方法的用户,您感兴趣的不是使参数成为可选参数,而是在未声明变量的情况下不使用它。虽然这是不可能的,但 C# 7.0 可以在方法调用中声明它。因此,
int unusedValue;
TryGetValue("key", out unusedValue);
您不会得到:
TryGetValue("key", out int unusedValue);
甚至:
TryGetValue("key", out _);
这应该添加为答案,但是:
- 我不能对这个问题执行此操作,因为它被标记为欺骗;
- 这个问题似乎是一个骗局,实际上要求的是不同的东西。
Let's assume I have a function with out
parameter, however I do not need its value. Is there a way to pass no actual parameter if given result will be thrown away anyway?
EDIT:
Although the question has been voted to be dupe of Optional Output Parameters
it only is if you look from the method creator's perspective. If you're the user of the method, you're interested not in making the parameter optional, just not using it without declaring the variable. And while this is not possible, with C# 7.0 it is possible to declare it in method call. So instead of:
int unusedValue;
TryGetValue("key", out unusedValue);
you get:
TryGetValue("key", out int unusedValue);
or even:
TryGetValue("key", out _);
This should be added as an answer, but:
- I can't do this for this question, because it was marked as a dupe;
- the question this question is seemed as a dupe of actually asks for a different thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定 C#,但在 VB.Net 中,您可以简单地将常量传递给输出(byref)参数。因此,如果您有一个整数作为输出参数,则不必传入实际变量,只需传入 0 或任何其他有效整数即可。对于对象,包括字符串,您只需传入 Nothing(C# 中为 Null)即可,一切正常。我不确定变量存储在哪里,可能只是像您传入的任何其他参数一样存储在堆栈上,并且当函数退出时它会消失。
Not sure about C#, but in VB.Net you can simply pass in a constant to an output (byref) parameter. So if you have an integer, as an output parameter, you don't have to pass in an actual variable, you can just pass in 0, or any other valid integer. For Objects, including strings you can just pass in Nothing (Null in C#) and everything works fine. I'm not sure where the variable is stored, probably just on the stack as in any other parameter you pass in, and it disappears when the function exits.
你不能这样做,但没有规则说你必须使用返回的值。您可以简单地传递一个不再使用的临时变量。
C# 4.0 允许可选参数,但
out
参数不能是可选的。编辑:顺便说一句,您还可以重载该方法:
You cannot do this, but there's no rule saying that you have to use the value that comes back. You can simply pass a temporary variable that you never use again.
C# 4.0 allows optional parameters, but
out
parameters can't be optional.EDIT: BTW, you can also overload the method:
虽然您实际上无法将 out 参数设置为可选,但您可以简单地为不带 out 参数的函数创建一个重载,这样就无需创建临时变量。
编辑:这些天使用丢弃未使用的变量是理想(特定于
out
的文档),当您向自己/其他开发人员传达您故意忽略该变量时。您还可以内联未使用的变量。为什么不呢,我们也使用 lambda 语法吧。这几乎消除了进行隐藏
out
参数的重载的需要,因为在调用代码中,您可以使用out int _
将方法调用写成一行。While you can't actually make the out parameter optional, you could simply create an overload for the function without the out parameter, which would then take away the need to create a temporary variable.
EDIT: These days using discards for unused variables is ideal (documentation specific to
out
), as you communicate to yourself/other developers that you are deliberately ignoring the variable. You can also inline the unused variable. And why not, let's use lambda syntax as well.This almost eliminates the need for making an overload at all that hides the
out
parameter, since in your calling code you can one-line the method call without int _
.