AmbiguousMatchException Constructors

Definition

Initializes a new instance of the AmbiguousMatchException class.

Overloads

AmbiguousMatchException()

Initializes a new instance of the AmbiguousMatchException class with an empty message string and the root cause exception set to null.

AmbiguousMatchException(String)

Initializes a new instance of the AmbiguousMatchException class with its message string set to the given message and the root cause exception set to null.

AmbiguousMatchException(String, Exception)

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.

AmbiguousMatchException()

Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs

Initializes a new instance of the AmbiguousMatchException class with an empty message string and the root cause exception set to null.

C#
public AmbiguousMatchException();

Remarks

AmbiguousMatchException inherits from Exception. This constructor sets the properties of the Exception object as shown in the following table.

Property Value
InnerException null
Message The empty string ("").

See also

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AmbiguousMatchException(String)

Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs

Initializes a new instance of the AmbiguousMatchException class with its message string set to the given message and the root cause exception set to null.

C#
public AmbiguousMatchException(string message);
C#
public AmbiguousMatchException(string? message);

Parameters

message
String

A string indicating the reason this exception was thrown.

Remarks

AmbiguousMatchException inherits from Exception. This constructor sets the properties of the Exception object as shown in the following table.

Property Value
InnerException null
Message The message string.

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AmbiguousMatchException(String, Exception)

Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs
Source:
AmbiguousMatchException.cs

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.

C#
public AmbiguousMatchException(string message, Exception inner);
C#
public AmbiguousMatchException(string? message, Exception? inner);

Parameters

message
String

The error message that explains the reason for the exception.

inner
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.

Examples

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

C#
using System;
using System.Reflection;

namespace Ambiguity
{
    class Myambiguous
    {
        //The first overload is typed to an int
        public static void Mymethod(int number)
        {
           Console.WriteLine("I am from 'int' 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()
        {
            try
            {
                //The following does not cause as exception
                Mymethod(2);    // goes to Mymethod(int)
                Mymethod("3");  // goes to Mymethod(string)

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

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

                //Invoke a method, utilizing a int 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);
            }
            catch
            {
                Console.WriteLine("\nSome other exception.");
            }
            return;
        }
    }
}

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

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

Remarks

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.

Property Value
InnerException The inner exception reference.
Message The error message string.

See also

Applies to

.NET 10 och andra versioner
Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0