C# 为什么 Visual Studio 默认不创建公共类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/824555/
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
Why Visual Studio doesn't create a public class by default?
提问by Vadim
In Visual Studio when you add a new class, it always created with no modifiers and that makes class internal.
在 Visual Studio 中,当你添加一个新类时,它总是在创建时没有修饰符,这使得类成为内部的。
class MyClass
{
}
I would prefer that my class by default is created as public one.
我希望我的课程默认创建为公共课程。
Why is it internal by default?
为什么默认是内部的?
What would you prefer?
你更喜欢什么?
采纳答案by Anton Gogolev
Making class internal
by default makes perfect sense to me: keep your privates to yourself and only explicitly expose parts which really need to be exposed: everything else is just implementation details and should not be visible to the outside world.
internal
默认情况下创建类对我来说非常有意义:保持你自己的隐私,只显式公开真正需要公开的部分:其他一切都只是实现细节,不应该对外界可见。
In case you want to test your internal
classes, .NET 2.0 onwards introduces a new attribute called InternalsVisibleToAttribute, which
如果你想测试你的internal
类,.NET 2.0 以后引入了一个名为InternalsVisibleToAttribute的新属性,它
Specifies that types that are ordinarily visible only within the current assembly are visible to another assembly.
指定通常仅在当前程序集中可见的类型对另一个程序集可见。
If this really annoys you, see %ProgramFiles%\Microsoft Visual Studio 8\Common7 IDE\ItemTemplates\CSharp\1033\Class.zip
. This is a template which you can change to suit your needs. ReSharper has similar capability, but it's directly accessible from within th UI.
如果这真的让您烦恼,请参阅%ProgramFiles%\Microsoft Visual Studio 8\Common7 IDE\ItemTemplates\CSharp\1033\Class.zip
。这是一个模板,您可以根据自己的需要进行更改。ReSharper 具有类似的功能,但它可以直接从用户界面中访问。
回答by Michael Haren
C# tends to default everything to the minimum scope necessary. This is a nice convention and quoted in Skeet's book (C# In Depth, p 224 "Note/Trivia"):
C# 倾向于将所有内容默认为必要的最小范围。这是一个很好的约定,并在 Skeet 的书中引用(C# In Depth,p 224 "Note/Trivia"):
[Properties are the] only place where “private” is required—Everywhere else in C#, the default access modifier in any given situation is the most private one possible. In other words, if something can be declared to be private, then leaving out the access modifiers entirely will default it to being private. This is a nice element of language design, because it's hard to get it wrong accidentally: if you want something to be more public than it is, you'll notice when you try to use it.
[属性是] 唯一需要“私有”的地方——在 C# 的其他任何地方,任何给定情况下的默认访问修饰符都是可能的最私有的。换句话说,如果某些东西可以被声明为私有的,那么完全省略访问修饰符将默认它是私有的。这是语言设计的一个很好的元素,因为它很难被意外弄错:如果你想要一些比它更公开的东西,当你尝试使用它时你会注意到。
回答by Winston Smith
Because in C#, a class is internal by default. VisualStudio is therefore following the C# specification.
因为在 C# 中,类在默认情况下是内部的。因此,VisualStudio 遵循 C# 规范。
The following article explains access modifiers and default visibility in C#.
以下文章解释了 C# 中的访问修饰符和默认可见性。
回答by Pondidum
Interestingly this only happens in C# - in vb.net you get a Public Class by default.
有趣的是,这只发生在 C# 中——在 vb.net 中,默认情况下你会得到一个公共类。
Personally, i prefer a public class by default as generally other classes need access to it. (most of my work is in the data layer though)
就个人而言,默认情况下我更喜欢公共类,因为通常其他类需要访问它。(不过我的大部分工作都在数据层中)
回答by TWith2Sugars
You could change the templates for a C# class - Usually located in "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#"
您可以更改 C# 类的模板 - 通常位于“C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#”
or even create your own template.
甚至创建自己的模板。
回答by BenAlabaster
I prefer it the way it is. This way you have to consciously decide that you want it exposed to public. It's much like the argument, do you want your computer open to the outside world by default or would you rather configure it for internet access yourself.
我更喜欢它的样子。通过这种方式,您必须有意识地决定要向公众公开。这很像争论,您希望您的计算机默认对外部世界开放,还是您愿意自己配置它以访问互联网。
Both approaches have their advantages, one has major potential security implications that I believe you should be aware of and make a conscious decision about rather than just it just happening automatically.
这两种方法都有其优点,一种具有重大的潜在安全隐患,我相信您应该意识到并做出有意识的决定,而不仅仅是它自动发生。
回答by danswain
Personally I prefer it as is, It forces you to actively think which classes you want to make public.
我个人更喜欢原样,它迫使您积极思考要公开哪些课程。
It ultimately defaults you into a hopefully cleaner API design and hence better more friendly software. You wouldn't want to expose the inner workings of your code, which would inevitably happen if everything defaulted to public.
它最终会让你默认采用更简洁的 API 设计,从而更好地更友好的软件。您不想公开代码的内部工作原理,如果一切都默认为公开,这将不可避免地发生。
This is somewhat subjective, personally I prefer everything to off and turn on only what I need not the other way round.
这有点主观,我个人更喜欢关闭所有东西,只打开我需要的东西,而不是相反。
回答by Dave Cousineau
It is good practice in all areas of code to keep things as restricted as possible. Without specific reason for being otherwise, everything should be private, readonly (better yet const), and static. If a variable, method, or property can be private, make it private. If it can't be private, but can be protected, make it protected. If it can be readonly, make it readonly. If it can be static, make it static. The same is true for classes: they should be internal by default, and made public only when you have decided that this is a class you wish to export.
在代码的所有领域都尽可能限制事物是一种很好的做法。没有特别的理由,一切都应该是私有的、只读的(最好是常量)和静态的。如果变量、方法或属性可以是私有的,请将其设为私有。如果它不能是私有的,但可以被保护,那就让它受到保护。如果它可以是只读的,请将其设为只读。如果它可以是静态的,那就让它成为静态的。类也是如此:默认情况下它们应该是内部的,并且只有在您确定这是您希望导出的类时才公开。
回答by Aaron Hoffman
To create a Public class by default for Visual Studio 2012:
默认情况下为 Visual Studio 2012 创建一个 Public 类:
Edit this file: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
编辑此文件: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
To look like this:
看起来像这样:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
public class $safeitemrootname$
{
}
}
More info: http://aaron-hoffman.blogspot.com/2013/05/edit-default-visual-studio-2012-item.html
更多信息:http: //aaron-hoffman.blogspot.com/2013/05/edit-default-visual-studio-2012-item.html