简体   繁体   English

为什么类需要默认构造函数,但结构却不需要?

[英]Why class required default constructor but structure dont required?

Look into following code 看下面的代码

public class ABC
{
    public ABC(int a)
    {
    }
}

public struct XYZ
{
    public XYZ(int a)
    {
    }
}

public class Test
{
    //This is invalid.
    ABC _abc = new ABC();

    //This is valid. Why?
    XYZ _xyz = new XYZ();
}

Why struct dont required default constructor where as class required same? 为什么struct不需要默认的构造函数,而class需要相同的构造函数?

There is always a parameterless constructor in a struct - and you can't define your own one. 在结构中始终有一个无参数的构造struct -您不能定义自己的构造struct It will always initialize all fields to their default values. 它将始终将所有字段初始化为其默认值。 This is effectively a requirement of the CLR, although the CLR itself doesn't refer to this as a constructor, and is described in section 11.3.8 of the C# spec. 这实际上是CLR的要求,尽管CLR本身并未将其称为构造函数,并在C#规范的11.3.8节中进行了描述。 (Although C# doesn't allow you to declare your own parameterless constructor for structs, the CLR does - and it's sometimes called. See my blog post on the topic for more information.) (尽管C#不允许您为结构声明自己的无参数构造函数,但CLR却允许- 有时也被称为。有关更多信息,请参阅我的博客文章 。)

The value created by calling the parameterless constructor on a struct is always the same as an "uninitialized" value in an array or an instance/static field. 通过调用所涉及的参数构造创造的价值struct始终是相同的如在阵列或一个实例/静磁场一个“未初始化的”值。

Classes, however, have a different "default" value, as a field (or array element) of a reference type will be null by default. 但是,类具有不同的“默认”值,因为引用类型的字段(或数组元素)默认情况下为null There is no guaranteed way of creating an instance of a class without specifying any values. 没有指定任何值就无法保证创建类实例的方法。 If you specify any constructors yourself for a class, the C# compiler will not provide a default constructor, as described in section 10.11.4 of the C# spec. 如果您自己为类指定任何构造函数,则C#编译器将不提供默认构造函数,如C#规范的10.11.4节所述。

Because the default constructor of a class (reference type) is only exposed if an explicit one is not implemented (that is, it doesn't exist). 因为仅在未实现显式类(即不存在)的情况下才公开类(引用类型)的默认构造函数。 A struct (value type) doesn't even need to be new 'd, so to speak. 可以说,结构(值类型)甚至不需要是new的。 You can use a variable that represents a struct without it - ie it won't be null , anyway (that is, it needn't exist at all). 您可以使用表示没有它的结构的变量-即无论如何它都不会为null (也就是说,它根本不需要存在)。

When a storage location (variable, field, array slot, etc.) of a class type is first created, it holds null . 首次创建类类型的存储位置(变量,字段,数组插槽等)时,其保存为null When a storage location of a structure type is first created, it holds an instance of the type in which every byte has been set to zero. 首次创建结构类型的存储位置时,它将保存该类型的实例,其中每个字节都设置为零。 Unlike C++, .NET provides no means for a type to exercise any say over when storage locations of that type may be created, nor what should happen when they are. 与C ++不同,.NET没有提供任何手段让类型在何时可以创建该类型的存储位置时行使任何发言权,也没有提供任何手段对它们进行声明。

If Foo is a class type and code creates an array bar = new Foo[100] , the array will be created with 100 slots that don't contain references to Foo (they're initially null ). 如果Foo是类类型,并且代码创建了一个数组bar = new Foo[100] ,则将使用100个不包含对Foo引用(它们最初为null )的插槽创建该数组。 Code that wants to make any array slot hold a reference to a Foo will have to somehow get its hands on one, and the only way any references to Foo will exist is if someone asks the class to create one (by calling its constructor). 想要使任何数组槽都持有对Foo的引用的代码将必须以某种方式获得对Foo引用,并且对Foo任何引用都将存在的唯一方法是,如果有人要求类创建一个(通过调用其构造函数)。

By contrast, if Moo is a structure type, creating an array boz = new Moo[100] , the array will be created with 100 slots, each one of which is a Moo instance . 相比之下,如果Moo是结构类型,则创建一个数组boz = new Moo[100] ,该数组将创建有100个插槽, 每个插槽都是一个Moo实例 Whereas a Foo can hold a value ( null ) which doesn't refer to an instance of Foo , none of the array slots will be capable of holding anything other than Moo instances. 尽管Foo可以保存一个不引用Foo实例的值( null ),但是除了Moo实例之外,所有数组插槽都不能容纳任何其他内容。 Since creating an array of Moo inherently creates instances of Moo without the type having any say in the matter, there's really no mechanism by which a struct type could assert control over instance creation. 由于创建数组Moo固有产生的情况下, Moo不必在这个问题上有发言权的类型,真的没有机制,结构类型可以断言在创建实例的控制。

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

相关问题 .NET中具有默认构造函数的必需属性 - Required properties in .NET with a default constructor 在泛型中使用必需的构造函数继承类 - Inheriting class with required constructor in generics 默认构造函数在不需要时不必要地初始化依赖项 - Default constructor unnecessarily initializing dependency when not required 将依赖项注入与必需的默认构造函数一起使用 - Using dependency injection with a required default constructor WCF [DataContract]类是否需要空白构造函数? 为什么? - Is a blank constructor required for WCF [DataContract] classes? Why? Jquery验证使用默认[必需]但不适用于自定义类 - Jquery Validation Works with Default [Required] but not with custom class 为什么在具有主构造函数的记录中需要显式的“this”构造函数初始化程序? - Why is an explicit `this` constructor initializer required in records with a primary constructor? 为什么我的子类需要使用默认参数覆盖? - Why is my subclass required to override with default parameters? Dapper'需要一个无参数的默认构造函数或一个匹配的签名' - Dapper 'A parameterless default constructor or one matching signature is required' 如何使用所需的构造函数参数为类创建契约类? - How to create contract class for class with required constructor arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM