VB.NET 类成员默认公开
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14724374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET class members public by default
提问by Patrick J Collins
According to MSDN:
根据MSDN:
Public access is the normal level for a programming element when you do not need to limit access to it. Note that the access level of an element declared within an interface, module, class, or structure defaults to Public if you do not declare it otherwise.
当您不需要限制对它的访问时,公共访问是编程元素的正常级别。请注意,如果您不以其他方式声明,则在接口、模块、类或结构中声明的元素的访问级别默认为 Public。
So, if I declare a class method in VB.NET without specifying an access modifier, then it is public by default:
因此,如果我在 VB.NET 中声明一个类方法而不指定访问修饰符,则默认情况下它是公共的:
Sub DoSomething()
End Sub
This is insane! I want members to be private by default, and only those specifically marked as Public to be visible outside the class. Like in C#... How do I modify this behaviour?
疯了吧!我希望成员在默认情况下是私有的,并且只有那些特别标记为 Public 的成员才能在类外可见。就像在 C# 中一样......我如何修改这种行为?
回答by Tim Schmelter
This is insane! I want members to be private by default
疯了吧!我希望成员默认为私有
As Fredrik has already commented, you should always provide explicit access modifiers.
正如 Fredrik 已经评论过的,您应该始终提供显式访问修饰符。
The code will be much more clear for other readers if you always explicitly include the access modifier.
如果您始终明确包含访问修饰符,则其他读者的代码将更加清晰。
I assume that this is due to downwards compatibility or developers who are not familiar with access modifiers at all.
我认为这是由于向下兼容性或根本不熟悉访问修饰符的开发人员。
But you are right, as in C# I would suggest to make everything as private as possible by default. You can make it more public when needed.
但是您说得对,就像在 C# 中一样,我建议默认情况下尽可能将所有内容设为私有。您可以在需要时使其更加公开。
Declaration Contexts and Default Access Levels (VB.NET)
Any idea how to modify this behaviour?
知道如何修改这种行为吗?
I don't think that it's possible to specify the default access modifier somewhere in Visual Studio. You could try to create a template-class which is suggested here (not tested):
我认为不可能在 Visual Studio 的某处指定默认访问修饰符。您可以尝试创建此处建议的模板类(未测试):
Visual C# 2010 Express: Specify default access modifier for new classes?

