.NET life

HUGON Jérôme
Microsoft Certified Technology Specialist Microsoft Certified Application Developer Microsoft Certified Professional

C# 4 Optional parameters: Conflict Resolution

.NET

Category .NET  | Publication Date : 4/11/2010

Optional parameters open new possibilities for C# developers, it can cause conflicts if the method call is equivalent to several signatures.

Take for example the following methods:

public void myMethod(int i) {}

public void myMethod(int i, bool b = false) {}

public void myMethod(object o) {}

What method will be called with the following statement?

myObject.myMethod(1);

The three methods correspond. The rule is that the method executed is the one whose signature best matches the parameters passed, taking into account the number of parameters and their types.
The method that will be called will be:

public void myMethod(int i) {}


Related articles

Book: C# 4 Develop Windows applications with Visual Studio 2010

Visual Studio

Category Visual Studio  | Publication Date : 12/6/2010

C# 4 Named Parameters

.NET

Category .NET  | Publication Date : 4/11/2010

C# 4 Optional Parameters

.NET

Category .NET  | Publication Date : 4/11/2010