C# keyword similar to “this” but for current class type?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 18:37:37

问题


Is there a keyword to refer to the current class type similar to how this refers to the current instance?

My intention is to avoid having to type the full class name when refering to static members or Enums but I want to prefex it with something like "this" for readability like I do for instance members. The reason being some of the class names are pretty huge and cause excessive wrapping.


回答1:


I don't think there's a keyword, but maybe as good:

this.GetType();

GetType is one of the few methods implemented by System.Object.




回答2:


If you include the namespace with using, you won't indeed to enter the full path.

You can alias the class names with using, for instance:

using ClassA = Really.Deep.Class.In.Namepsace.For.SomePurpose.ClassA;



回答3:


typeof(this) will do what you're after.

Edit: I stand corrected. typeof(this) isn't valid C#. dbaseman is correct: this.GetType() will work.



来源:https://stackoverflow.com/questions/13677237/c-sharp-keyword-similar-to-this-but-for-current-class-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!