超越私有类成员的范围限制 [英] Restricting scope of a class member beyond private
本文介绍了超越私有类成员的范围限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道是否有一种方法,以类成员的范围,进入专门的get / set实现在C#中,以减少我的不小心直接访问他们的机会。像私人使用,但只允许get / set方法来访问它,我在想,我可以换每个变量在自己的类,但似乎矫枉过正我要找的限制。谢谢
I was wondering if there was a way to scope access of a class member specifically to get/set implementations in c# to reduce the chance of me accidentally accessing them directly. Something like private but only allows get/set to access it, I was thinking that I could wrap each variable in their own class but that seems overkill for the restriction I'm looking for. Thanks
推荐答案
没有,可惜没有。我以为你会是这样的后:
No, unfortunately not. I assume you'd be after something like this:
// Not actually valid C#!
public string Name
{
// Only accessible within the property
string name;
get { return name; }
set
{
if (value == null)
{
throw new ArgumentNullException();
}
name = value;
}
}
我想这样的事情之前,太。不幸的是这是不可能的:(
I've wanted something like this before, too. Unfortunately it's not possible :(
这篇关于超越私有类成员的范围限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文