Analysis of multi-loaded constructors and null parameters in C#
Suppose a class contains multiple constructors:
<code class="language-csharp">public class EffectOptions { public EffectOptions(params object[] options) { } public EffectOptions(IEnumerable<object> options) { } public EffectOptions(string name) { } public EffectOptions(object owner) { } public EffectOptions(int count) { } public EffectOptions(Point point) { } }</code>
When calling a constructor with a null value, such as EffectOptions options = new EffectOptions(null);
, the overload resolution system will follow the following steps:
1. Identify applicable constructors:
2. Exclude unapplicable constructors:
3. Parse params constructor:
4. Identify the best applicable candidates:
In this case, the ambiguity arises because:
object[]
and string
are both considered "specific" (there is no more specific in between). Algorithm details:
The above is the detailed content of How Does C# Resolve Overloaded Constructors When a Null Value is Passed?. For more information, please follow other related articles on the PHP Chinese website!