简体   繁体   English

C#:为什么我必须在类的变量中使用公共访问修饰符?

[英]C#: Why do I have to use the public access modifier in class's vars?

OK, well, I am a beginner, so.. yeah, this may be a very stupid question.好吧,好吧,我是初学者,所以.. 是的,这可能是一个非常愚蠢的问题。 I read that if I declare variable or object, without mentioniong the access-modifier (public, private, etc.) than it's automatically making it having the Internal acess modifier (and it will exist anywhere in the current namespace).我读到,如果我声明变量或 object,而不提及访问修饰符(公共、私有等),它会自动使其具有内部访问修饰符(它将存在于当前命名空间中的任何位置)。

So why do I need to set my vars in a class as Public to get them in another class (such as my program's class of course).那么为什么我需要将 class 中的变量设置为 Public 以将它们放入另一个 class (例如我的程序的 class 当然)。

Because you can not read?因为看不懂? Ok, joking aside.好吧,开玩笑的。 You said you are a eginner, so this is totally normal to overlook small things.你说你是初学者,所以忽略小事是完全正常的。

I read that if I declare variable or object, without mentioniong the access-modifier (public, private, etc.) than it's automatically making it having the Internal acess modifier我读到,如果我声明变量或 object,而不提及访问修饰符(公共、私有等),它会自动使其具有内部访问修饰符

Ah, no.啊,不。 It defaults to private , NOT to internal.它默认为私有,而不是内部。 It defaults to the most sensible default, and internal would still allow a lot of cross class accesses that lead to bad code practices.它默认为最合理的默认值,内部仍然允许大量交叉 class 访问,这会导致错误的代码实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM