为什么C#2.0引入了静态类? [英] Why C# 2.0 introduced static class ?

查看:47
本文介绍了为什么C#2.0引入了静态类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是很想知道为什么c#2.0引入了静态类?我读到某个地方,在静态类之前,每个人都使用私有构造函数来做同样的工作,这是真的吗?如果不是真正的原因?



编辑1:在发布这个问题之后我再次寻找答案,并在这个链接中找到几条好线 - 点击此处



谢谢



我的尝试:



谷歌搜索但无法找到任何答案。

I am just curious to know that why c# 2.0 introduced static class ? I read somewhere that before static class everyone was using private constructor to do same work, it is true? if not than what was the actual reason?

Edit 1 : After posting this question I was again searing for answer and find out few good lines in this link - click here

Thanks

What I have tried:

Googled but could not able to find any answer for this.

推荐答案

引入静态类以提供类无法实例化或继承。虽然您可以创建一个Singleton类来防止存在多个实例,但在V2.0之前无法强制执行它。静态类通过拒绝允许实例成员或实例构造函数来提供强制执行 - 如果尝试,则会出现编译错误。因为您无法创建实例,所以只能通过className.memberName语法访问这些方法,并且您无法创建引用静态类的变量。

稍后(在V3.0)它们用于保存扩展方法,但这些在V2.0中不可用



有关示例,请参阅MSDN:静态类和静态类成员(C#编程指南) [ ^ ]
Static classes were introduced to provide classes that can't be instantiated or inherited from. While you can create a Singleton class to prevent more than one instance existing, before V2.0 there was no way to enforce it. Static classes provide that enforcement by refusing to allow instance members or instance constructors - if you try, you will get a compilation error. Because you can't create an instance you can only access the methods via the className.memberName syntax, and you can't create a variable to reference a static class.
Later (at V3.0) they were used to hold Extension Methods, but these weren't available in V2.0

See MSDN for examples: Static Classes and Static Class Members (C# Programming Guide)[^]


静态类是一种简单方便的方法来强制执行任何非静态成员。如果您尝试添加任何非静态成员,并且该类本身保持静态,则会收到错误消息。此外,静态类不允许继承。所以,它部分回答了你的问题:到目前为止,这与防止类的实例化不一样,这是别的。



另请注意,你是什么描述,添加一个私有的无参数(重要!)构造函数,只是为了消除具有默认构造函数的效果)。当然,你不会添加任何其他构造函数:添加一个私有的构造函数是没有意义的,添加任何非私有的构造函数将允许实例化。



现在,这是功能真的需要语法功能吗?当然不是。没有它,一切都会奏效。所以呢?一切都可以没有属性,模板,访问修饰符,使用语句...等等。您需要了解语言中的许多功能都适用于:编译器的万无一失的行为,更好的编译器诊断,便利性,维护,表达能力,可读性等。另请参阅:语法糖 - 维基百科,免费的百科全书



请理解:您的问题类型为什么在设计中有这个和那个?如果它允许在为什么不呢?中给出答案,那么可能效率不高样式。 :-)



-SA
Static class is a simple convenient method to enforce the absence of any non-static members. If you try to add any non-static member, and the class itself remains static, you get a error message. Also, inheritance is not allowed for the static class. So, it answers partially your question: this is, by far, not the same as preventing instantiation of a class, this is something else.

Also note that what you describe, adding a private parameteless (important!) constructor, is done just to remove the effect of having the default constructor). Naturally, you would not add any other constructors: adding a private one would make no sense, and adding any non-private one would allow instantiation.

Now, is this syntactic feature really needed for the functionality? Of course not. Everything would work without it. So what? Everything could work without properties, templates, access modifiers, using statement… and a lot more. You need to understand that many features in language are good for: fool-proof behavior of the compiler, better compiler diagnostics, convenience, maintenance, expressive capabilities, readability, and so on. See also: Syntactic sugar — Wikipedia, the free encyclopedia.

Please understand: your question of the type "why having this and that in the design?" maybe not really productive if it allows the answer in the "why not?" style. :-)

—SA


这篇关于为什么C#2.0引入了静态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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