简体   繁体   中英

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

Not really 2 constructors, rather 3 but 2 of them are overloaded.

  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>() :

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>() '

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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