问题
When I provide a constructor with a default value
public MyClass(string description = null) { .... }
is this equivalent to
public MyClass() { .... }
public MyClass(string description) { .... }
in terms of Serialization. In other words, is a default constructor available? Practically it is, but will I face some issues when I use serialization?
回答1:
No. It unfortunately is not a default constructor.
When you write:
public MyClass(string description = null) { .... }
You're actually making a constructor that accepts a string parameter, but has an attribute marking the default value for that attribute. This is different than having a default constructor on the class.
回答2:
Yes, you will have problems in that case.
I've tried to call a constructor described by you via reflection, and it thrown TargetInvokationException: (Argument count mismatch).
来源:https://stackoverflow.com/questions/8012558/c-sharp-default-value-in-constructor-same-as-two-constructors-for-serialization