将值传递给方法
假设您有:
public void TestFishsticks()
{
var fishes = GetFishstick(false);
}
private object GetFishstick(bool getBigFishes)
{
return FishsticksManager().GetFishsticks(getBigFishes);
}
vs
public void TestFishsticks()
{
var fishes = GetFishstick(getBigFishes: false);
}
private object GetFishstick(bool getBigFishes)
{
return FishsticksManager().GetFishsticks(getBigFishes);
}
这有什么原因吗?
在我目前的公司代码库中,我们似乎两者都做,但似乎没有理由选择其中之一。我可以看到第二个选择在可读性上有一个小小的改进,因为你可以立即看到参数名称,但无论如何你都可以通过智能感知看到它?
So let's say you have:
public void TestFishsticks()
{
var fishes = GetFishstick(false);
}
private object GetFishstick(bool getBigFishes)
{
return FishsticksManager().GetFishsticks(getBigFishes);
}
vs
public void TestFishsticks()
{
var fishes = GetFishstick(getBigFishes: false);
}
private object GetFishstick(bool getBigFishes)
{
return FishsticksManager().GetFishsticks(getBigFishes);
}
Is there any reason for this?
In my current companies codebase we seem to do both, but there seems to be no reason for one over the other. I could see a small readability improvement with the second choice, because you get to see the parameter name straight away, but you can see it via intellisense anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C# 4.0 中引入命名参数主要是为了提高可读性。您实际上不必使用它们。有些人喜欢在某些情况下使用它们,有些人则不喜欢。这基本上取决于你。
它可以极大地提高可读性,特别是当您不想在一直阅读代码时触发智能感知(或者更糟糕的是,查看打印输出)时。比较这两者:
但是,同时使用命名参数和可选参数使您可以仅为可选参数列表中的几个参数提供参数。例如,此功能极大地方便了对 COM 接口的调用。
Named arguments have mainly been introduced in C# 4.0 to improve readability. You don't actually have to use them. Some people prefer using them in some cases, others don't. It's basically up to you.
It can greatly improve readability, especially when you don't want to trigger intellisense when reading code all the time (or even worse, reviewing print-outs). Compare these two:
However, using named arguments and optional parameters together enables you to supply arguments for only a few parameters from a list of optional parameters. This capability for instance greatly facilitates calls to COM interfaces.
使用命名参数的一些充分理由:
代码一目了然,特别是当参数为
false
或null
或0
或""
等等。命名参数与可选参数配合得很好。如果您只需要指定其中的几个参数,则采用十几个参数的方法可以具有简化的调用站点。
该代码在早期开发过程中面对重新排序重构时非常稳健。如果您在向客户发布之前进行了重大更改,请更改:
则
所有所述代码
仍然正确,但所述代码
需要更新。
类似地,这使得代码在面对更改此指定矩形的方法时变得健壮:
对此方法:
这是一个明显的重大更改。 当方法改变时,代码
会报错。此代码不会,并且会改变行为:
Some good reasons to use named arguments:
The code is easier to understand at a glance, particularly when the argument is
false
ornull
or0
or""
and so on.Named arguments work well with optional arguments. A method that takes a dozen arguments can have a simplified call site if you just need to specify a few of them.
The code is robust in the face of reordering refactorings during early development. If you make a breaking change before releasing to customers, changing:
to
then all the code that said
will still be correct, but the code that said
will need to be updated.
Similarly, this makes code robust in the face of changing this method that specifies a rectangle:
to this method:
an obvious breaking change. Code
will give an error when the method changes. This code will not, and changes behaviour:
是的,这主要是为了可读性。当您有一长串可选参数时,它也会派上用场,例如;
还应该注意的是,智能感知并不总是可用。例如,在差异查看器(如 WinMerge)或偶尔的记事本中阅读代码。即使智能感知不存在,通过使用命名参数仍然具有可读性。
Yes, it is mostly a readability thing. It can also come in handy when you have a long list of optional parameters, say;
It should also be noted that intellisense is not always available. For example, reading code in a diff viewer like WinMerge, or occasionally notepad. The readability is still there by using the named parameters even though intellisense isn't.
命名参数帮助我们不必记住或查找被调用方法的参数列表中的参数顺序。每个参数的参数可以通过参数名称指定。 (就个人而言)这没有意义,因为你只有一个参数。如果您有多个,那么它将有助于更改顺序或快速可读性(如果您不关心方法智能感知)
Named arguments helps us not to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. It doesn’t make sense (personally) since you have one parameter. In case if you have multiple then it will help to change the order or quick readability (if you don’t care about method intellisense)
您在这里使用的是命名参数。
它们可以极大地增加可读性。特别是对于布尔值。
想一想:
如果你阅读,
你就会知道更多。它可以避免这么多混乱。作为奖励,您可以按照自己喜欢的方式调用它:
命名参数仅适用于 C#4 及以上版本。
What you are using here are named arguments.
They can greatly add to readability. Especially for booleans.
Consider:
If instead you read
you know a whole lot more. It can avoid so much confusion. As a bonus, you can call it any way you like:
Named arguments are only available form C#4 onwards.