Mono Class Library: System Namespace

System.ArrayTypeMismatchException Class

Represents the error that occurs when an attempt is made to store an element of the wrong type in an array. [Edit]

See Also: ArrayTypeMismatchException Members

System.Object
     System.Exception
          System.SystemException
               System.ArrayTypeMismatchException

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

Thread Safety

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

Remarks

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 int values, but an element of type string cannot be stored in a int array because conversion between these types is not supported.

Note:

This exception is thrown by the Array.Copy(Array, Array, int) 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 CIL instructions throw ArrayTypeMismatchException :

  • ldelem.<type>
  • ldelema
  • stelem.<type>
[Edit]

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