简体   繁体   English

这两个C#类构造函数有什么区别?

[英]What is the difference between these two C# class constructors?

Not really 2 constructors, rather 3 but 2 of them are overloaded. 不是真正的2个构造函数,而是3个,但是其中2个是重载的。

  1. This one was originaly on some tutorial that I watched a while back. 这是我前一段时间观看的一些教程的原始内容。

      public Animal() { this.height=0; this.weight=0; this.name="No Name"; this.sound = "No Sound"; } public Animal(double height, double weight, string name, string sound) { this.height=height; this.weight=weight; this.name=name; this.sound = sound; } 
  2. I remember this from software dev classes, so I was wondering if it made any sense to write it this way, and if not what could go wrong if this were on my code? 我从软件开发人员类中记住了这一点,所以我想知道以这种方式编写它是否有意义,如果不是,那么如果在我的代码中这会出错吗?

      public Animal(double height=0, double weight=0, string name="No Name", string sound="No Sound") { this.height=height; this.weight=weight; this.name=name; this.sound = sound; } 

Because I really don't know if I can put default values in class constructor like this. 因为我真的不知道是否可以将默认值放在这样的类构造函数中。

I would say it's mainly about communication. 我想说的主要是交流。 In the first case you either provide all the values, or nothing(..and you get the defaults). 在第一种情况下,您要么提供所有值,要么不提供任何值(..并获得默认值)。 The person who wrote that code may have had a good reason for only those two options. 编写该代码的人可能只有两个选择才有充分的理由。

In the second case you are more flexible, for instance you could choose to provide only one parameter.. it could be the case that such an object would become invalid. 在第二种情况下,您会更加灵活,例如,您可以选择仅提供一个参数。这种情况可能会使此类对象变得无效。 As with most things.. it all depends 和大多数事情一样,这一切都取决于

Sure you can. 你当然可以。 And for most code you won't see a big difference (other than in the first case you'll have to supply either all of the parameters, or none). 对于大多数代码,您不会看到很大的区别(除了在第一种情况下,您将不必提供所有参数,也可以不提供任何参数)。

But there is a subtle difference nevertheless, and it can bite you later on. 但是仍然存在细微的差异,以后可能会咬你。

See, the first snippet declares a so-called default constructor and a custom one, but the second snippet only declares a custom constructor. 请参阅,第一个代码段声明了一个所谓的默认构造函数和一个自定义构造函数,但是第二个代码段仅声明了一个自定义构造函数。

Here's a generic function: 这是一个通用函数:

public static T Create<T>()
    where T : new()
{
    return new T();
}

Here's what you'll get if you call Create<Animal>() : 如果调用Create<Animal>()将得到以下内容:

error CS0310: ' Animal ' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method ' Program.Create<T>() ' 错误CS0310:“ Animal ”必须是具有公共无参数构造函数的非抽象类型,才能在通用类型或方法“ Program.Create<T>() ”中将其用作参数“ T”

So your custom constructor does not replace a default constructor in cases where it's expected. 因此,在预期的情况下,您的自定义构造函数不会替换默认的构造函数。

Same goes for reflection: 反思也是如此:

Activator.CreateInstance<Animal>();

Will result in a runtime exception: 将导致运行时异常:

Unhandled Exception: System.MissingMethodException : No parameterless constructor defined for this object. 未处理的异常: System.MissingMethodException :没有为此对象定义无参数的构造函数。

So providing an accessible default constructor has also a semantic meaning. 因此,提供可访问的默认构造函数也具有语义。 It tells an instance of your class is still meaningful when created through its default constructor, and doesn't require additional parameters. 它告诉您的类实例在通过其默认构造函数创建时仍然有意义,并且不需要其他参数。 It can be used in scenarios involving serialization for instance (some serializers require a default constructor, other don't). 例如,它可用于涉及序列化的场景(某些序列化程序需要默认构造函数,而其他则不需要)。

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

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