ArrayTypeMismatchException (Clase)
Ensamblado: mscorlib (en mscorlib.dll)
Se produce ArrayTypeMismatchException cuando el sistema no puede convertir el elemento en el tipo declarado para la matriz. Por ejemplo, no se puede almacenar un elemento de tipo String en una matriz Int32 porque la conversión entre estos tipos no es compatible. Producir esta excepción, por lo general, resulta innecesario en el caso de aplicaciones.
Las siguientes instrucciones máquina del Lenguaje intermedio de Microsoft (MSIL) producen ArrayTypeMismatchException:
-
ldelem.<type>
-
ldelema
-
stelem.<type>
ArrayTypeMismatchException utiliza HRESULT COR_E_ARRAYTYPEMISMATCH, que tiene el valor 0x80131503.
Para obtener una lista con los valores de propiedad iniciales de una instancia de ArrayTypeMismatchException, vea los constructores ArrayTypeMismatchException.
En el siguiente ejemplo de código se muestran escenarios donde se produce la excepción ArrayTypeMismatchException.
using System; namespace ArrayTypeMismatch { class Class1 { static void Main(string[] args) { string[] names = {"Dog", "Cat", "Fish"}; Object[] objs = (Object[]) names; try { objs[2] = "Mouse"; foreach (object animalName in objs) { System.Console.WriteLine(animalName); } } catch (System.ArrayTypeMismatchException) { // Not reached; "Mouse" is of the correct type. System.Console.WriteLine("Exception Thrown."); } try { Object obj = (Object) 13; objs[2] = obj; } catch (System.ArrayTypeMismatchException) { // Always reached, 13 is not a string. System.Console.WriteLine( "New element is not of the correct type."); } // Set objs to an array of objects instead of // an array of strings. objs = new Object[3]; try { objs[0] = (Object) "Turtle"; objs[1] = (Object) 12; objs[2] = (Object) 2.341; foreach (object element in objs) { System.Console.WriteLine(element); } } catch (System.ArrayTypeMismatchException) { // ArrayTypeMismatchException is not thrown this time. System.Console.WriteLine("Exception Thrown."); } } } }
package ArrayTypeMismatch;
import System.*;
class Class1
{
public static void main(String[] args)
{
String names[] = { "Dog", "Cat", "Fish" };
Object objs[] = names;
try {
objs.set_Item(2, "Mouse");
for (int iCtr = 0; iCtr < objs.length; iCtr++) {
Console.WriteLine(objs[iCtr]);
}
}
catch (System.ArrayTypeMismatchException exp) {
// Not reached, "Mouse" is of the correct type.
Console.WriteLine("Exception Thrown.");
}
try {
objs[2] = (Int32)13;
}
catch (System.ArrayTypeMismatchException exp) {
// Always reached, 13 is not a string.
Console.WriteLine(("New element is not of the correct type."));
}
// Set objs to an array of objects instead of an array of strings.
objs = new Object[3];
try {
objs.set_Item(0, "Turtle");
objs.set_Item(1, (Int16)12);
objs.set_Item(2, (Single)2.341);
for (int iCtr = 0; iCtr < objs.length; iCtr++) {
Console.WriteLine(objs[iCtr]);
}
}
catch (System.ArrayTypeMismatchException exp) {
// ArrayTypeMismatchException is not thrown this time.
System.Console.WriteLine("Exception Thrown.");
}
} //main
} //Class1
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.
Referencia
ArrayTypeMismatchException (Miembros)System (Espacio de nombres)
Exception