VB.NET 类成员默认公开 [英] VB.NET class members public by default

查看:25
本文介绍了VB.NET 类成员默认公开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MSDN:

公共访问是编程元素的正常级别,当您不需要限制对它的访问.请注意,访问级别在接口、模块、类或结构中声明的元素如果您不另行声明,则默认为 Public.

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.

因此,如果我在 VB.NET 中声明一个类方法而不指定访问修饰符,则默认情况下它是公共的:

So, if I declare a class method in VB.NET without specifying an access modifier, then it is public by default:

Sub DoSomething()

End Sub

这太疯狂了!我希望成员在默认情况下是私有的,并且只有那些特别标记为 Public 的成员才能在类外可见.就像在 C# 中一样......我如何修改这种行为?

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?

推荐答案

这太疯狂了!我希望成员默认为私有

This is insane! I want members to be private by default

正如 Fredrik 已经评论过的,您应该始终提供显式访问修饰符.

As Fredrik has already commented, you should always provide explicit access modifiers.

如果你总是这样,其他读者的代码会更清楚显式包含访问修饰符.

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.

但你是对的,就像在 C# 中一样,我建议默认情况下尽可能将所有内容设为私有.您可以在需要时使其更加公开.

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.

声明上下文和默认值访问级别 (VB.NET)

知道如何修改这种行为吗?

Any idea how to modify this behaviour?

我认为不可能在 Visual Studio 的某处指定默认访问修饰符.您可以尝试创建此处建议的模板类(未测试):

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 C# 2010 Express:为新类指定默认访问修饰符?

这篇关于VB.NET 类成员默认公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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