The exception that is thrown when an attempt is made to store an element of the wrong type within an array.
See Also: ArrayTypeMismatchException Members
ArrayTypeMismatchException is thrown when the system cannot convert the element to the type declared for the array. For example, an element of type string cannot be stored in an int array because conversion between these types is not supported. It is generally unnecessary for applications to throw this exception.
The following Microsoft intermediate language (MSIL) instructions throw ArrayTypeMismatchException :
ldelem.<type>
ldelema
stelem.<type>
ArrayTypeMismatchException uses the HRESULT COR_E_ARRAYTYPEMISMATCH, which has the value 0x80131503.
For a list of initial property values for an instance of ArrayTypeMismatchException, see the ArrayTypeMismatchException constructors.
The following example demonstrates an error that causes the system to throw a ArrayTypeMismatchException exception.
C# Example
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.