使用或不使用“this” C# 中的限定符
可能的重复:
你什么时候使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最好,我仅使用
this
来防止(可能)属性和函数参数之间出现歧义。如果您已经在某个类的上下文中工作,那么记住这一点并不难,并且我不需要被提醒这样一个事实:每次我寻址一个局部变量时,我都在寻址一个局部变量。
所以我认为 ReSharper 在这一点上是正确的。
Preferably, i use
this
only to prevent ambiguity between (possibly) a property and a function parameterIf 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.
要记住的重要事情之一是
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.我发现它是多余的,特别是对于定义良好的编码标准:
使用
this.
似乎需要更多工作。I find it redundant particularly with a well defined coding standard:
Using
this.<whatever>
just seems to be more work.我个人认为使用
this
关键字是一个很好的做法,因为它清楚地标记了 prorerty/method/etc 是否属于对象实例。这在命名私有成员时特别有用,以便局部变量与私有成员无法区分:不用说,根本不使用它可能比不一致地使用更好。
然而,我发现不断使用
this
会使代码看起来“嘈杂”,而this
是无用的干扰因素。 IMO 最好对私有成员使用特殊的_
前缀命名:它的输入速度也更快。
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: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" withthis
being unuseful distractor. IMO it's better to use special_
prefix naming for private members:it's also much faster to type.
使用这个的一个好处是。是智能感知。它缩小了您可以更快选择的事物列表。
One advantage of using this. is intellisense. It narrows the list of things you could pick faster.
它在技术上是多余的,但 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.
在这种情况下,它是一种偏好 - 所以你更喜欢使用它,告诉 resharper,它就会停止抱怨。
In this case its a preference - so you prefer to use it, tell resharper that and it will stop complaining.