什么是私有类成员的正确使用关键字'这个'? [英] What is the proper use of keyword 'this' in private class members?
本文介绍了什么是私有类成员的正确使用关键字'这个'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在应关键字'这个'将C#类定义中使用?
When should the keyword 'this' be used within C# class definitions?
它是标准使用的形式this.Method()从类中?或者只是使用方法()?我已经看到两者,通常与第二个选择去,但我想更多地了解这个问题。
Is it standard to use the form "this.Method()" from within class? Or to just use "Method()"? I have seen both, and usually go with the second choice, but I would like to learn more about this subject.
推荐答案
最的时间,它是多余的,可以省略;有一些例外:
Most of the time it is redundant and can be omitted; a few exceptions:
- 来调用链构造:
美孚():这个(巴){}
- 来本地参数/变量和外地之间的歧义:
this.foo = foo的;
等 - 要调用的扩展在当前实例方式:
this.SomeMethod();
(其中定义为公共静态的someMethod(这个富富){...}
) - 要对当前实例的引用传递给外部方法:
Helper.DoSomething(本);
- to call a chained constructor:
Foo() : this("bar") {}
- to disambiguate between a local argument/variable and a field:
this.foo = foo;
etc - to call an extension method on the current instance:
this.SomeMethod();
(where defined aspublic static SomeMethod(this Foo foo) {...}
) - to pass a reference to the current instance to an external method:
Helper.DoSomething(this);
这篇关于什么是私有类成员的正确使用关键字'这个'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文