AmbiguousMatchException Constructor (String, Exception)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Initializes a new instance of the AmbiguousMatchException class with a specified error message and a reference to the inner exception that is the cause of this exception.

Namespace:   System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

public AmbiguousMatchException(
	string message,
	Exception inner


Type: System.String

The error message that explains the reason for the exception.

Type: System.Exception

The exception that is the cause of the current exception. If the inner parameter is not null, the current exception is raised in a catch block that handles the inner exception.

An exception that is thrown as a direct result of a previous exception should include a reference to the previous exception in the InnerException property. The InnerException property returns the same value that is passed into the constructor, or null if the InnerException property does not supply the inner exception value to the constructor.

The following table shows the initial property values for an instance of AmbiguousMatchException.




The inner exception reference.


The error message string.

The following example shows two classes, each named Mymethod. One class takes an integer and the other takes a string. If an integer is passed to Mymethod, the first class is used. If a string is passed, the second class is used. If it cannot be determined which Mymethod to use, AmbiguousMatchException is thrown.

using System;
using System.Reflection;

namespace Ambiguity
    class Myambiguous
        //The first overload is typed to an Int32
        public static void Mymethod(Int32 number)
           Console.WriteLine("I am from 'Int32' method");

        //The second overload is typed to a string
        public static void Mymethod(string alpha)
            Console.WriteLine("I am from 'string' method.");

        public static void Main()
                //The following does not cause as exception
                Mymethod(2);    // goes to Mymethod(Int32)
                Mymethod("3");  // goes to Mymethod(string)

                Type Mytype = Type.GetType("Ambiguity.Myambiguous");

                MethodInfo Mymethodinfo32 = Mytype.GetMethod("Mymethod", new Type[]{typeof(Int32)});
                MethodInfo Mymethodinfostr = Mytype.GetMethod("Mymethod", new Type[]{typeof(System.String)});

                //Invoke a method, utilizing a Int32 integer
                Mymethodinfo32.Invoke(null, new Object[]{2});

                //Invoke the method utilizing a string
                Mymethodinfostr.Invoke(null, new Object[]{"1"});

                //The following line causes an ambiguious exception
                MethodInfo Mymethodinfo = Mytype.GetMethod("Mymethod");
            }   // end of try block
            catch (AmbiguousMatchException ex)
                Console.WriteLine("\n{0}\n{1}", ex.GetType().FullName, ex.Message);
                Console.WriteLine("\nSome other exception.");

//This code produces the following output:
// I am from 'Int32' method
// I am from 'string' method.
// I am from 'Int32' method
// I am from 'string' method.

// System.Reflection.AmbiguousMatchException
// Ambiguous match found.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top