将值传递给方法

发布于 2024-12-09 14:04:27 字数 570 浏览 2 评论 0原文

假设您有:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

池予 2024-12-16 14:04:27

C# 4.0 中引入命名参数主要是为了提高可读性。您实际上不必使用它们。有些人喜欢在某些情况下使用它们,有些人则不喜欢。这基本上取决于你。

它可以极大地提高可读性,特别是当您不想在一直阅读代码时触发智能感知(或者更糟糕的是,查看打印输出)时。比较这两者:

CalculateBMI(123, 178); // What do the numbers mean?
CalculateBMI(weightInKg: 123, heightInCentimeters: 178); // Clearer IMHO.

但是,同时使用命名参数和可选参数使您可以仅为可选参数列表中的几个参数提供参数。例如,此功能极大地方便了对 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:

CalculateBMI(123, 178); // What do the numbers mean?
CalculateBMI(weightInKg: 123, heightInCentimeters: 178); // Clearer IMHO.

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.

温柔嚣张 2024-12-16 14:04:27

似乎没有理由将其中之一凌驾于另一个之上

使用命名参数的一些充分理由:

代码一目了然,特别是当参数为 falsenull0"" 等等。

命名参数与可选参数配合得很好。如果您只需要指定其中的几个参数,则采用十几个参数的方法可以具有简化的调用站点。

该代码在早期开发过程中面对重新排序重构时非常稳健。如果您在向客户发布之前进行了重大更改,请更改:

void M(int width, int height)

void M(int height, int width)

所有所述代码

M(height: 123, width: 456);

仍然正确,但所述代码

M(123, 456);

需要更新。

类似地,这使得代码在面对更改此指定矩形的方法时变得健壮:

M(int top, int bottom, int left, int right)

对此方法:

M(int top, int height, int left, int width)

这是一个明显的重大更改。 当方法改变时,代码

M(top: 10, bottom: 20, left: 30, width: 40)

会报错。此代码不会,并且会改变行为:

M(10, 20, 30, 40);

there seems to be no reason for one over the other

Some good reasons to use named arguments:

The code is easier to understand at a glance, particularly when the argument is false or null or 0 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:

void M(int width, int height)

to

void M(int height, int width)

then all the code that said

M(height: 123, width: 456);

will still be correct, but the code that said

M(123, 456);

will need to be updated.

Similarly, this makes code robust in the face of changing this method that specifies a rectangle:

M(int top, int bottom, int left, int right)

to this method:

M(int top, int height, int left, int width)

an obvious breaking change. Code

M(top: 10, bottom: 20, left: 30, width: 40)

will give an error when the method changes. This code will not, and changes behaviour:

M(10, 20, 30, 40);
只是在用心讲痛 2024-12-16 14:04:27

是的,这主要是为了可读性。当您有一长串可选参数时,它也会派上用场,例如;

public bool GetFishstick(int x = 1, int y = 2, int z = 3)
{
...
}

// Called as such: The other optional parameters (x,y) are supplied automatically.
var fish = GetFishstick(z: 10);

// Compare to the alternative where you have to provide them.
var fish = GetFishstick(1,2,10);

还应该注意的是,智能感知并不总是可用。例如,在差异查看器(如 WinMerge)或偶尔的记事本中阅读代码。即使智能感知不存在,通过使用命名参数仍然具有可读性。

Yes, it is mostly a readability thing. It can also come in handy when you have a long list of optional parameters, say;

public bool GetFishstick(int x = 1, int y = 2, int z = 3)
{
...
}

// Called as such: The other optional parameters (x,y) are supplied automatically.
var fish = GetFishstick(z: 10);

// Compare to the alternative where you have to provide them.
var fish = GetFishstick(1,2,10);

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.

恍梦境° 2024-12-16 14:04:27

命名参数帮助我们不必记住或查找被调用方法的参数列表中的参数顺序。每个参数的参数可以通过参数名称指定。 (就个人而言)这没有意义,因为你只有一个参数。如果您有多个,那么它将有助于更改顺序或快速可读性(如果您不关心方法智能感知)

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)

把梦留给海 2024-12-16 14:04:27

您在这里使用的是命名参数

它们可以极大地增加可读性。特别是对于布尔值。

想一想:

var date = GetDate(2011, 2, 3);  // 'feb 3' or '2nd of march' ? 

如果你阅读,

var date = GetDate(year:2011, month:2, day:3); 

你就会知道更多。它可以避免这么多混乱。作为奖励,您可以按照自己喜欢的方式调用它:

var date = GetDate(month:2, day:3, year:2011);  // same date.

命名参数仅适用于 C#4 及以上版本。

What you are using here are named arguments.

They can greatly add to readability. Especially for booleans.

Consider:

var date = GetDate(2011, 2, 3);  // 'feb 3' or '2nd of march' ? 

If instead you read

var date = GetDate(year:2011, month:2, day:3); 

you know a whole lot more. It can avoid so much confusion. As a bonus, you can call it any way you like:

var date = GetDate(month:2, day:3, year:2011);  // same date.

Named arguments are only available form C#4 onwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文