阻止访问私有成员变量?强制使用公共财产? [英] Blocking access to private member variables? Force use of public properties?

查看:79
本文介绍了阻止访问私有成员变量?强制使用公共财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET 2.0,所以没有访问自动属性的权限.因此,我必须采用以下方式对私有变量和公共属性进行编码

I'm using .NET 2.0 so do not have access to automatic properties. So I must resort to the following way of coding private variables and public properties

private string m_hello = null;

public string Hello
{
     get{return m_hello;}
     set{m_hello = value;}
}

对于上述私有/公共成员的包含类的方法,是否有限制访问私有变量的方法?我不喜欢使用m_hello或Hello.

For methods of the containing class of the above private/public members, is there anyway to restrict access to the private variable? I do not like that I can either use m_hello or Hello.

谢谢.

推荐答案

除了简单地遵循自己的约定并执行此操作外,没有其他方法可以使用.

No there is not a way to do that, other than to simply follow your own convention and do this.Hello if you really need to go through your public property.

我也不明白为什么也需要/想要这样做,因为它是您的内部类,因此您可以控制代码,并且可以定义使用什么/如何使用它,因此应该没问题.

I don't see why you would need/want to do this either, as since it is your internal class, you are the one in control of the code and you can define what/how it is used, so there shouldn't be an issue.

这篇关于阻止访问私有成员变量?强制使用公共财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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