本文整理汇总了C#中System.ArrayTypeMismatchException类的典型用法代码示例。如果您正苦于以下问题:C# ArrayTypeMismatchException类的具体用法?C# ArrayTypeMismatchException怎么用?C# ArrayTypeMismatchException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ArrayTypeMismatchException类属于System命名空间,在下文中一共展示了ArrayTypeMismatchException类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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.");
}
}
}
}