您更喜欢用什么习语(如果有的话)来命名“这个”? C# 中扩展方法的参数,为什么?

发布于 2024-07-16 04:30:46 字数 606 浏览 5 评论 0原文

C# 扩展方法的第一个参数是调用扩展方法的实例。 我采用了一种习惯用法,将这个变量称为“self”,但在其他地方没有看到过。 如果其他人也使用它,我一点也不感到惊讶。 这是一个例子:

public static void Print(this string self)
{
   if(self != null) Console.WriteLine(self);
}

但是,我开始看到其他人将该参数命名为“@this”,如下所示:

public static void Print(this string @this)
{
   if(@this != null) Console.WriteLine(@this);
}

作为第三个选项,有些人根本不喜欢任何习语,说“self”和“@this”不提供任何信息。 我想我们都同意有时参数有一个明确的、有意义的名称,特定于其目的,这比“self”或“@this”更好。 有些人更进一步说,你总是可以想出一个更有价值的名字。 所以这是另一个有效的观点。

你还见过哪些成语? 您更喜欢什么习语,为什么?

The first parameter to a C# extension method is the instance that the extension method was called on. I have adopted an idiom, without seeing it elsewhere, of calling that variable "self". I would not be surprised at all if others are using that as well. Here's an example:

public static void Print(this string self)
{
   if(self != null) Console.WriteLine(self);
}

However, I'm starting to see others name that parameter "@this", as follows:

public static void Print(this string @this)
{
   if(@this != null) Console.WriteLine(@this);
}

And as a 3rd option, some prefer no idiom at all, saying that "self" and "@this" don't give any information. I think we all agree that sometimes there is a clear, meaningful name for the parameter, specific to its purpose, which is better than "self" or "@this". Some go further and say you can always come up with a more valuable name. So this is another valid point of view.

What other idioms have you seen? What idiom do you prefer, and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

梅倚清风 2024-07-23 04:30:46

我根据用途相当正常地命名它。 因此,“source”表示 LINQ 运算符的源序列,或者“argument”/“parameter”表示进行参数/参数检查的扩展等。

我认为它不必与“this”或“self”特别相关" - 没有提供有关参数含义的任何额外信息。 当然这是最重要的事情。

编辑:即使在没有太多明显含义的情况下,我也更喜欢一些含义而不是。 “self”或“@this”赋予了哪些信息? 只是它是扩展方法中的第一个参数 - 并且通过该参数用 this 修饰的事实,该信息已经很明显了。 在给出 theStringToPrint/self 选项的示例中,我将使用 outputText 代替 - 它传达了您需要了解的有关参数的所有信息,国际海事组织。

I name it fairly normally, based on the use. So "source" for the source sequence of a LINQ operator, or "argument"/"parameter" for an extension doing parameter/argument checking, etc.

I don't think it has to be particularly related to "this" or "self" - that doesn't give any extra information about the meaning of the parameter. Surely that's the most important thing.

EDIT: Even in the case where there's not a lot of obvious meaning, I'd prefer some meaning to none. What information is conferred by "self" or "@this"? Merely that it's the first parameter in an extension method - and that information is already obvious by the fact that the parameter is decorated with this. In the example case where theStringToPrint/self option is given, I'd use outputText instead - it conveys everything you need to know about the parameter, IMO.

与君绝 2024-07-23 04:30:46

如果变量是一个普通的旧静态方法,我将按照我的命名方式准确命名该变量。 原因是它仍然可以作为静态方法调用,并且您必须在代码中考虑该用例。

查看此问题的最简单方法是参数验证。 考虑将 null 传递到您的方法中的情况。 您应该进行参数检查并抛出 ArgumentNullException。 如果它正确实现,您需要将“this”作为参数名称,如下所示。

public static void Print(this string @this) {
  if ( null == @this ) {
    throw new ArgumentNullException("this");
  }
  ...
}

现在有人正在针对您的库进行编码,突然出现一个异常对话框,其中显示“这是空的”。 他们会感到最困惑:)

这是一个有点人为的例子,但总的来说,我对待扩展方法与普通的旧静态方法没有什么不同。 我发现这让他们更容易推理。

I name the variable exactly how I would name it if it were a plain old static method. The reason being that it can still be called as a static method and you must consider that use case in your code.

The easiest way to look at this is argument validation. Consider the case where null is passed into your method. You should be doing argument checking and throwing an ArgumentNullException. If it's implemented properly you'll need to put "this" as the argument name like so.

public static void Print(this string @this) {
  if ( null == @this ) {
    throw new ArgumentNullException("this");
  }
  ...
}

Now someone is coding against your library and suddenly gets an exception dialog which says "this is null". They will be most confused :)

This is a bit of a contrived example, but in general I treat extension methods no different that a plain old static method. I find it makes them easier to reason about.

初见终念 2024-07-23 04:30:46

我见过使用 obj 和 val 。 我不喜欢这个。 我们应该尽量避免使用关键字。 我从未见过自己,但我喜欢它。

I have seen obj and val used. I do not like @this. We should try to avoid using keywords. I have never seen self but I like it.

别低头,皇冠会掉 2024-07-23 04:30:46

我将其称为“目标”,因为扩展方法将对该参数进行操作。

I call it 'target', since the extension method will operate on that parameter.

一抹微笑 2024-07-23 04:30:46

我认为应该避免@this,因为它使用了有史以来最无用的特定于语言的功能(@)。 事实上,任何可能导致混乱或降低可读性的内容(例如关键字出现在非关键字的地方)都应该避免。
self 让我想起了 python,但可能有利于一致的命名约定,因为很明显它指的是正在使用的实例,而不需要一些讨厌的语法技巧。

I believe @this should be avoided as it makes use of the most useless language-specific feature ever seen (@). In fact, anything that can cause confusion or decrease readability such as keywords appearing where they are not keywords should be avoided.
self reminds me of python but could be good for a consistent naming convention as it's clear that it's referring to the instance in use while not requiring some nasty syntactic trickery.

芸娘子的小脾气 2024-07-23 04:30:46

你可以做这样的事情......

public static void Print(this string extended)
{
   if(extended != null) Console.WriteLine(extended);
}

You could do something like this...

public static void Print(this string extended)
{
   if(extended != null) Console.WriteLine(extended);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文