public class ArrayTypeMismatchException : SystemException
Object
Exception
SystemException
ArrayTypeMismatchException
mscorlib
BCL
Represents the error that occurs when an attempt is made to store an element of the wrong type in an array.
ArrayTypeMismatchException is thrown when the system cannot convert the element to the type declared for the array. [Note: This exception will not be thrown if the element can be converted to the type declared for the array. For example, an element of type Byte can be stored in an array declared to store Int32 values, but an element of type String cannot be stored in a Int32 array because conversion between these types is not supported.]
[Note: This exception is thrown by the System.Array.Copy(System.Array,System.Array,System.Int32) method if a widening conversion cannot be performed on the operand to convert it to the array type.
It is generally unnecessary for applications to throw this exception.
The following IL instructions throw ArrayTypeMismatchException :
]
- ldelem.<type>
- ldelema
- stelem.<type>
The following example demonstrates an error that causes the system to throw a ArrayTypeMismatchException exception.
using System; class ArrayTypeMisMatchExample { public static void Main() { string[] array1={"hello","world"}; int[] array2 = {1,2}; try { Array.Copy(array1,array2,2); } catch (ArrayTypeMismatchException e) { Console.WriteLine("Error: {0}",e); } } }The output is
Error: System.ArrayTypeMismatchException: Source array type cannot be assigned to destination array type.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at System.Array.Copy(Array sourceArray, Array destinationArray, Int32 length)
at ArrayTypeMisMatchExample.Main()
System Namespace
ArrayTypeMismatchException Constructors
ArrayTypeMismatchException() Constructor
ArrayTypeMismatchException(System.String) Constructor
ArrayTypeMismatchException(System.String, System.Exception) Constructor
public ArrayTypeMismatchException();
Constructs and initializes a new instance of the ArrayTypeMismatchException class.
This constructor initializes the System.ArrayTypeMismatchException.Message property of the new instance to a system-supplied message that describes the error, such as "Source array type cannot be assigned to destination array type." This message takes into account the current system culture.The System.ArrayTypeMismatchException.InnerException property is initialized to
null
.
System.ArrayTypeMismatchException Class, System Namespace
public ArrayTypeMismatchException(string message);
Constructs and initializes a new instance of the ArrayTypeMismatchException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
This constructor initializes the System.ArrayTypeMismatchException.Message property of the new instance using message. If message isnull
, the System.ArrayTypeMismatchException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.ArrayTypeMismatchException.InnerException property is initialized tonull
.
System.ArrayTypeMismatchException Class, System Namespace
public ArrayTypeMismatchException(string message, Exception innerException);
Constructs and initializes a new instance of the ArrayTypeMismatchException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- innerException
- An instance of Exception that is the cause of the current exception. If innerException is non-null, then the current exception was raised in a catch block handling innerException.
This constructor initializes the System.ArrayTypeMismatchException.Message property of the new instance using message , and the System.Exception.InnerException property using innerException. If message isnull
, the System.ArrayTypeMismatchException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.[Note: For information on inner exceptions, see System.Exception.InnerException.]
System.ArrayTypeMismatchException Class, System Namespace