使用或不使用“this” C# 中的限定符

发布于 2024-11-05 11:43:41 字数 412 浏览 1 评论 0原文

可能的重复:
你什么时候使用“this”关键字?

我刚刚开始使用 Resharper 来分析我的代码,它建议我做很多事情,我对它的输出非常满意,因为它还教会了我一些更好的做事方法。

显然它的建议可以忽略,其中之一我只想从社区获得一些反馈!

我一直更喜欢对属于当前类的属性(即 this.Name)使用“this”限定符。没有真正的原因——我似乎只是几个月前就采用了这个。

Resharper 认为这是多余的,也许我应该删除它。

你怎么认为?

Possible Duplicate:
When do you use the “this” keyword?

I have just started using Resharper to analyze my code and there are many things it has suggested that I do and I am really pleased with its output as it is also teaching me a few better methods for doing things.

Obviously its suggestions can be ignored and one of these I would just like to get some feedback from the community on!

I have always preferred to use the 'this' qualifier for properties that belong to the current class (i.e this.Name). No real reason - I just seemed to adopt this many moons ago.

Resharper suggests that this is redundant and maybe I should remove it.

What do you think?

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

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

发布评论

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

评论(7

但可醉心 2024-11-12 11:43:41

最好,我仅使用 this 来防止(可能)属性和函数参数之间出现歧义。

public class thing
{

   private string name;

   public thing(string name)
   {
       this.name = name; // will set private string name to param string name
   }

}

如果您已经在某个类的上下文中工作,那么记住这一点并不难,并且我不需要被提醒这样一个事实:每次我寻址一个局部变量时,我都在寻址一个局部变量。

所以我认为 ReSharper 在这一点上是正确的。

Preferably, i use this only to prevent ambiguity between (possibly) a property and a function parameter

public class thing
{

   private string name;

   public thing(string name)
   {
       this.name = name; // will set private string name to param string name
   }

}

If you are already working in the context of a certain class, it's not so hard to keep this in mind, and i do not need to be reminded of the fact that I am addressing a local variable every time i address one.

So I think ReSharper's right on this one.

说好的呢 2024-11-12 11:43:41

要记住的重要事情之一是 this 被编译器删除,因此这纯粹是“什么对您和与您共享代码的人来说看起来不错?”的问题。它不会影响性能一点点。

One of the important things to remember is that this is removed by the compiler and so it is purely a matter of 'what looks good to you and those with whom you share code?'. It will affect performance not a whit.

暮倦 2024-11-12 11:43:41

我发现它是多余的,特别是对于定义良好的编码标准:

Name // Property
_name // Member field
name // local variable

使用 this. 似乎需要更多工作。

I find it redundant particularly with a well defined coding standard:

Name // Property
_name // Member field
name // local variable

Using this.<whatever> just seems to be more work.

唔猫 2024-11-12 11:43:41

我个人认为使用 this 关键字是一个很好的做法,因为它清楚地标记了 prorerty/method/etc 是否属于对象实例。这在命名私有成员时特别有用,以便局部变量与私有成员无法区分:

/// some pretty long method
/// ...
frameCount += 1; // is it private memeber or some local defined above?

不用说,根本不使用它可能比不一致地使用更好。

然而,我发现不断使用 this 会使代码看起来“嘈杂”,而 this 是无用的干扰因素。 IMO 最好对私有成员使用特殊的 _ 前缀命名:

/// some pretty long method
/// ...
_frameCount += 1; // it's clearly private memeber!

它的输入速度也更快。

I personally think that it's a good practice to use this keyword because it's clearly marks does prorerty/method/etc belongs to object instance or not. This is especially useful then private members are named so that local variables are indistinguishable from private members:

/// some pretty long method
/// ...
frameCount += 1; // is it private memeber or some local defined above?

Needless to say it's probably better not to use it at all than use inconsistenly.

However I find that constant usage of this makes code look "noisy" with this being unuseful distractor. IMO it's better to use special _ prefix naming for private members:

/// some pretty long method
/// ...
_frameCount += 1; // it's clearly private memeber!

it's also much faster to type.

暗恋未遂 2024-11-12 11:43:41

使用这个的一个好处是。是智能感知。它缩小了您可以更快选择的事物列表。

One advantage of using this. is intellisense. It narrows the list of things you could pick faster.

别想她 2024-11-12 11:43:41

它在技术上是多余的,但 StyleCop(如果您很挑剔并且使用全套 Microsoft 编码标准)表示您应该使用它。

我这样做,很多人不这样做,所以我想这取决于个人喜好或雇主的编码标准。

It is technically redundant, but StyleCop (if you're being picky and using the full set of Microsoft coding standards) says you should use it.

I do, lots of people don't, so I guess it's down to personal preference or the coding standard at your employer.

神也荒唐 2024-11-12 11:43:41

在这种情况下,它是一种偏好 - 所以你更喜欢使用它,告诉 resharper,它就会停止抱怨。

In this case its a preference - so you prefer to use it, tell resharper that and it will stop complaining.

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