C# - 输出参数的语法糖?
让我们暂时假设,C# 在最纯粹的意义上允许多个返回值,我们期望看到类似以下内容:
string sender = message.GetSender();
string receiver = message.GetReceiver();
压缩为:
string sender, receiver = message.GetParticipants();
在这种情况下,我不必了解该方法的返回值,直到我实际执行方法调用。 也许我依靠智能感知来告诉我正在处理什么返回值,或者也许我正在寻找一种方法,该方法可以从我不熟悉的类中返回我想要的内容。
类似地,目前在 C# 中我们有这样的东西:
string receiver;
string sender = message.GetParticipants(out receiver);
其中 GetParticipants 的参数是一个 out 字符串参数。 然而,这与上面的有点不同,因为这意味着我必须抢占,或者至少返回并编写代码,创建一个变量来保存 out 参数的结果。 这有点违反直觉。
我的问题是,当前的 C# 中是否有任何语法糖,允许开发人员在与方法调用相同的行中进行此声明? 我认为这将使开发变得(微小)更加流畅,并且如果我做类似的事情,也会使代码更具可读性:
string sender = message.GetParicipants(out string receiver);
显示接收器正在现场声明和分配。
Let us say for a moment that C# allowed multiple return values in the most pure sense, where we would expect to see something like:
string sender = message.GetSender();
string receiver = message.GetReceiver();
compacted to:
string sender, receiver = message.GetParticipants();
In that case, I do not have to understand the return values of the method until I actually make the method call. Perhaps I rely on Intellisense to tell me what return value(s) I'm dealing with, or perhaps I'm searching for a method that returns what I want from a class I am unfamiliar with.
Similarly, we have something like this, currently, in C#:
string receiver;
string sender = message.GetParticipants(out receiver);
where the argument to GetParticipants is an out string parameter. However, this is a bit different than the above because it means I have to preempt with, or at least go back and write, code that creates a variable to hold the result of the out parameter. This is a little counterintuitive.
My question is, is there any syntactic sugar in current C#, that allows a developer to make this declaration in the same line as the method call? I think it would make development a (tiny) bit more fluid, and also make the code more readable if I were doing something like:
string sender = message.GetParicipants(out string receiver);
to show that receiver was being declared and assigned on the spot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
不,目前还没有任何语法糖。 我也没有听说有任何要介绍的意向。
我不能说我使用
out
参数的频率足够高,以至于它确实对我来说是一个重大问题(我希望 C# 团队花时间在其他功能上),但我同意这是一个有点烦人。No, there isn't currently any syntactic sugar around this. I haven't heard of any intention to introduce any either.
I can't say I use
out
parameters often enough for it really to be a significant concern for me (there are other features I'd rather the C# team spent their time on) but I agree it's a bit annoying..NET 4 将添加一个元组概念来处理这个问题。 不幸的是,C# 语言不会为“解构绑定”提供任何语言支持。
.NET 4 will be adding a Tuple concept, which deals with this. Unfortunately, the C# language isn't going to provide any language support for "destructuring bind".
就我个人而言,我喜欢使用 out 参数时带来的不便。 它帮助我思考我的方法是否真的做了它应该做的事情,或者我是否在其中塞入了太多功能。 也就是说,C#4.0/.Net 4 中的动态类型也许会解决您的一些担忧。
在哪里
Personally, I like the inconvience introduced when using out parameters. It helps me to think about whether my method is really doing what it should be or if I've crammed too much functionality into it. That said, perhaps dynamic typing in C#4.0/.Net 4 will address some of your concerns.
where
您还可以返回一个
Tuple
或类似的东西。 但是,由于您想返回两个字符串,因此可能会感到困惑。我使用 BclExtras 库 的元组结构,它非常方便(在 SO 上找到它,谢谢你贾里德帕!)。
You can also return a
Tuple<T,U>
or something similar. However, since you want to return two string, it might get confusing.I use the Tuples structs of the BclExtras library which is very handy (found it on SO, thank you JaredPar!).
我认为这样的功能不存在,但如果它以类似于 perl 中的数组的方式实现,那么实际上可能很有用。
在 perl 中,您可以将数组分配给括号中的变量列表。 所以你可以这样做
I don't think such functionality exists, but if it were implemented in a way similar to arrays in perl that could be useful actually.
In perl You can assign an array to a list of variables in parentheses. So you can for example do this
这最让我烦恼的地方:由于没有不带
out
参数的DateTime.TryParse
重载,因此如果不声明
就无法编写>d
。 我不知道这是out
参数的问题还是TryParse
方法的实现方式的问题,但这很烦人。Where this bugs me the most: since there's no overload of (say)
DateTime.TryParse
that doesn't take anout
parameter, you can't writewithout declaring
d
. I don't know if this is a problem without
parameters or just with how theTryParse
method is implemented, but it's annoying.这个语法糖现在可以在 roslyn 预览中使用如此处所示(称为声明表达式)。
This syntactic sugar is now is now available in the roslyn preview as seen here (called Declaration expressions).
最好的情况下,您必须使用 var 而不是显式类型,除非您希望将所有多个返回值限制为同一类型(不太实际)。 您还将限制变量的范围; 目前,您可以在更高的范围内声明变量并在输出参数中对其进行初始化。 使用这种方法,变量将超出与其赋值相同的块中的范围。 显然,这在某些情况下是有用的,但我不想将其作为一般规则强制执行。 显然,您可以保留“
out
”选项,但人们很可能会为一种方法或另一种方法编写代码。At best you would have to use
var
rather than an explicit type, unless you want to restrict all multiple return values to be of the same type (not likely practical). You would also be limiting the scope of the variable; currently you can declare a variable at a higher scope and initialize it in an out parameter. With this approach, the variable would go out of scope in the same block as its assignment. Obviously this is usable in some cases, but I wouldn't want to enforce this as the general rule. Obviously you could leave the 'out
' option in place, but chances are people are going to code for one approach or the other.我认为这不是你想要的。
您可能遇到过一段代码,您可能会遇到这样的情况:
喜欢那个。 但变量突然出现,因为
它们已在参数列表中引入
个人噩梦(对我来说:))
从这一点来看,多个返回值有严重的缺点
可移植性/可维护性。 如果你创建一个返回两个字符串的函数
现在你希望它返回 3,你将不得不更改所有代码
使用这个功能的。
然而,返回的记录类型通常在这种常见场景中表现良好。
您可能正在打开潘多拉魔盒;-)
对于行压缩:
行可以是任意长度,因此您可以将一些常见的东西打包到一个行中。
试着接受分号。
I think this is not what you want.
You may have come across a piece of code where you would have
liked that. But variables popping out of nowhere because
they have been introduced in the parameter list would be
a personal nightmare ( to me :) )
Multiple return values have grave downsides from the point
of portability/maintainability. If you make a function that returns two strings
and you now want it to return three, you will have to change all the code
that uses this function.
A returned record type however usually plays nice in such common scenarios.
you may be opening pandora's box ;-)
For line compacting:
Lines can be any length, so you could pack some common stuff into one.
Just try to live with the semicolons.
尝试下面的代码
Try the following code