简体   繁体   English

为什么MessageBox类在C#中没有默认构造函数?

[英]Why doesn't the MessageBox class have a default constructor in C#?

Case 1: 情况1:

I am trying this 我正在尝试这个

MessageBox m = new MessageBox();

And got compilation error 并得到编译错误

'System.Windows.Forms.MessageBox' has no constructors defined 'System.Windows.Forms.MessageBox'没有定义构造函数

Case 2: 案例2:
Then, I have made a class without constructor 然后,我创建了一个没有构造函数的类

class myClass
{

}

and tried myClass my = new myClass(); 并尝试了myClass my = new myClass(); This time I found no error. 这次我没有发现任何错误。

Now, my question: 现在,我的问题:

  • Why I am getting error in 1st case? 为什么我在第一种情况下收到错误?

Since, both are classes and every class have default constructor, then 因为,两个都是类,每个类都有默认构造函数

  • Where is default constructor in 1st case? 第一种情况下默认构造函数在哪里?

The constructor may be private or protected in order to forbid direct instantiation. 构造函数可以是privateprotected ,以禁止直接实例化。 Use the static factory method instead. 请改用静态工厂方法。 There is a static method Show in the MessageBox class. MessageBox类中有一个静态方法Show

Archil is right, too. Archil也是对的。 If theres a explicit constructor defined, the implicit default constructor is not created anymore. 如果定义了显式构造函数,则不再创建隐式默认构造函数。

And regarding x0ns comments: Yes, it's also impossible to instantiate static classes. 关于x0ns注释:是的,实例化静态类也是不可能的。 Don't use static classes, thats poor design (there are exceptions). 不要使用静态类,这是糟糕的设计(有例外)。

In c#, evey class automatically has default constructor if NONE is defined. 在c#中,如果定义了NONE,则evey类自动具有默认构造函数。 MessageBox defines other constructors, so it does not automatically have default constructor MessageBox定义了其他构造函数,因此它不会自动拥有默认构造函数

MessageBox is designed to be used as a static class - see http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx MessageBox旨在用作静态类 - 请参阅http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

You can make your class static using: 您可以使用以下方法使您的类静态:

static class myclass {}

System.Windows.Forms.MessageBox has no default (empty) constructor. System.Windows.Forms.MessageBox没有默认(空)构造函数。

A constructor can be hidden by setting its accessibility to something other than public. 可以通过将其可访问性设置为除public之外的其他内容来隐藏构造函数。

The class' design declares that you can't use it as an object. 类的设计声明你不能将它用作对象。
It only has static methods that can be used without instantiating an object of that class. 它只有静态方法,可以在不实例化该类对象的情况下使用。

In case 1, MessageBox is a static class, it has no constructors (update - it has a private constructor says reflector but the OP gave a misleading/incorrect compiler error message.) Static classes are defined like this: 在案例1中,MessageBox是一个静态类,它没有构造函数(更新 - 它有一个私有构造函数说反射器,但OP给出了一个误导/错误的编译器错误消息。)静态类定义如下:

public static class MessageBox { }

A static class can only have static methods, and as such is not meant to be instantiated. 静态类只能有静态方法,因此不能实例化。

In case 2, MyClass is not a static class, and the compiler generates a default constructor for you if you don't define any constructors. 在案例2中,MyClass不是静态类,如果您没有定义任何构造函数,编译器会为您生成默认构造函数。

UPDATE: to all downvoters: go compile up a project with a static class and examine it in reflector - it decompiles without the static keyword becuase there is no MSIL or metadata for a static class; 更新:对所有downvoters:使用静态类编译项目并在反射器中检查它 - 它在没有static关键字的情况下反编译,因为没有静态类的MSIL或元数据; the compiler (in .net 2.0 or later) generates an abstract sealed class with no constructors. 编译器(在.net 2.0或更高版本中)生成一个没有构造函数的抽象密封类。 The keyword "static" is just syntactic sugar. 关键字“静态”只是语法糖。 Additionally, in 1.0/1.1 of .NET (when MessageBox was created,) the static keyword did not exist for classes and the sealed/private ctor was the accepted pattern. 此外,在1.0 / 1.1的.NET(创建MessageBox时)中,类不存在static关键字,而密封/私有ctor是可接受的模式。

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

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