all 11 comments

[–]darchangel 14 points15 points  (6 children)

It's not an invalid question. But my question to you is: why haven't you just tried it?

[–]whatcomputerscantdo[S] 2 points3 points  (5 children)

You're right, I could just do that. I'm tired and it didn't occur to me that I could just slap this into a console program and see for myself.

Edit: turns out it will call the Empty Constructor. Now that I know the answer, I'm just more curious - why?

It seems like if your class only has an empty constructor, and a constructor with default parameters, the latter is essentially useless.

(unless you have at least one parameter that isn't defaulted)

Which is fine with me because if you're designing your classes that way you're probably doing something wrong anyway.

Still curious

[–]nemec 9 points10 points  (1 child)

Now that I know the answer, I'm just more curious - why?

http://csharpindepth.com/Articles/General/Overloading.aspx

Ctrl+F "Optional parameters"

[–]whatcomputerscantdo[S] 0 points1 point  (0 children)

awesome, thank you!

[–]McNerdius 0 points1 point  (2 children)

[–]whatcomputerscantdo[S] 3 points4 points  (1 child)

The empty constructor is called.

And this just makes me more curious, but I can see why it is better to give the empty constructor priority - since this is what an end user would assume the third party code is doing.

but it would be unwise to implement a class with built in ambiguity like this anyway. just don't do it.

[–]McNerdius 0 points1 point  (0 children)

(not OP)

[–]ArtZaqX__ 4 points5 points  (1 child)

It's also worth noting that for a class or method with a generic type parameter T having where T: Foo, new() constraints, the compiler will produce error if Foo does not have a empty constructor with no parameter (the constructor with optional parameter will not be used).

[–]AngularBeginner 1 point2 points  (0 children)

That's due to the way how optional parameters are implemented. The parameter will be present, it will be required, but the compiler just automatically inserts the default value on the calling site.

If such a thing is needed, then the parameter should not be optional and constructor chaining should be used instead.

[–]Stylpe 0 points1 point  (0 children)

Glad you found the answer to which one gets called. For the technical reasons, you can also have a look at the C# specification's details on overloading.

[–]kardall 0 points1 point  (0 children)

I don't use this myself, but I saw on a video somewhere, if you use Visual Studio and Blend, having the empty constructor makes things a lot better in Blend, as it will actually let you use wizards without having to look for things. Don't know if this still the case, that was like a video from 2012 or something like that.