System.ArrayTypeMismatchException Class

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

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class ArrayTypeMismatchException : SystemException

Remarks

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 :

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.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

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.
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()

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0