System.ArrayTypeMismatchException Class

public class ArrayTypeMismatchException : SystemException

Base Types

Object
  Exception
    SystemException
      ArrayTypeMismatchException

Assembly

mscorlib

Library

BCL

Summary

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

Description

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 CIL instructions throw ArrayTypeMismatchException :

]

Example

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

See Also

System Namespace

Members

ArrayTypeMismatchException Constructors

ArrayTypeMismatchException() Constructor
ArrayTypeMismatchException(System.String) Constructor
ArrayTypeMismatchException(System.String, System.Exception) Constructor


ArrayTypeMismatchException() Constructor

public ArrayTypeMismatchException();

Summary

Constructs and initializes a new instance of the ArrayTypeMismatchException class.

Description

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 .

See Also

System.ArrayTypeMismatchException Class, System Namespace

ArrayTypeMismatchException(System.String) Constructor

public ArrayTypeMismatchException(string message);

Summary

Constructs and initializes a new instance of the ArrayTypeMismatchException class.

Parameters

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.

Description

This constructor initializes the System.ArrayTypeMismatchException.Message property of the new instance using message. If message is null , 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 to null .

See Also

System.ArrayTypeMismatchException Class, System Namespace

ArrayTypeMismatchException(System.String, System.Exception) Constructor

public ArrayTypeMismatchException(string message, Exception innerException);

Summary

Constructs and initializes a new instance of the ArrayTypeMismatchException class.

Parameters

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.

Description

This constructor initializes the System.ArrayTypeMismatchException.Message property of the new instance using message , and the System.Exception.InnerException property using innerException. If message is null , 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.]

See Also

System.ArrayTypeMismatchException Class, System Namespace