在C#中的成员访问时,`this`关键字可选? [英] Is `this` keyword optional when accessing members in C#?

查看:109
本文介绍了在C#中的成员访问时,`this`关键字可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,如果你有一个私有成员中的一类,可以通过单指它的名称来访问它的类中的方法。你不必说了 this.memberName ,只是成员名的作品。那么,this关键字可选的成员访问的背景下?

I notice that if you have a private member in a class, you can access it in the class methods by just referring to it's name. You do not need to say this.memberName, just memberName works. So is the this keyword optional in the context of member access?

我看的时候,你要澄清的范围是非常有用的 - 当你有2个变量具有相同的名称。是否有访问成员时使用的任何其他原因?

I do see it is useful when you want to clarify the scope - when you have 2 variables with the same name. Is there any other reason to use it when accessing members?

推荐答案

是的,它是可选的。唯一的时候,你不得不使用它是当你有一个隐藏的成员变量的局部变量,或者要引用到的索引属性(又名索引)

Yes, it's optional. The only times you'd have to use it are when you have a local variable that hides a member variable, or you want to refer to an indexed property (aka indexer).

这篇关于在C#中的成员访问时,`this`关键字可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆