使用 as 和 is 运算符安全地进行强制转换
2015-11-05 16:38
393 查看
由于对象是多态的,因此基类类型的变量可以保存派生类型。 若要访问派生类型的方法,需要将值强制转换回该派生类型。 不过,在这些情况下,如果只尝试进行简单的强制转换,会导致引发 InvalidCastException 的风险。 这就是 C# 提供 is 和 as 运算符的原因。 您可以使用这两个运算符来测试强制转换是否会成功,而没有引发异常的风险。 通常,as 运算符更高效一些,因为如果可以成功进行强制转换,它会实际返回强制转换值。 而 is 运算符只返回一个布尔值。 因此,如果只想确定对象的类型,而无需对它进行实际强制转换,则可以使用 is 运算符。
下面的示例演示如何使用 is 和 as 运算符从一个引用类型强制转换为另一个引用类型,而没有引发异常的风险。 此示例还演示如何对 as 运算符使用可以为 null 值的类型。
下面的示例演示如何使用 is 和 as 运算符从一个引用类型强制转换为另一个引用类型,而没有引发异常的风险。 此示例还演示如何对 as 运算符使用可以为 null 值的类型。
class SafeCasting { class Animal { public void Eat() { Console.WriteLine("Eating."); } public override string ToString() { return "I am an animal."; } } class Mammal : Animal { } class Giraffe : Mammal { } class SuperNova { } static void Main() { SafeCasting app = new SafeCasting(); // Use the is operator to verify the type. // before performing a cast. Giraffe g = new Giraffe(); app.UseIsOperator(g); // Use the as operator and test for null // before referencing the variable. app.UseAsOperator(g); // Use the as operator to test // an incompatible type. SuperNova sn = new SuperNova(); app.UseAsOperator(sn); // Use the as operator with a value type. // Note the implicit conversion to int? in // the method body. int i = 5; app.UseAsWithNullable(i); double d = 9.78654; app.UseAsWithNullable(d); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } void UseIsOperator(Animal a) { if (a is Mammal) { Mammal m = (Mammal)a; m.Eat(); } } void UseAsOperator(object o) { Mammal m = o as Mammal; if (m != null) { Console.WriteLine(m.ToString()); } else { Console.WriteLine("{0} is not a Mammal", o.GetType().Name); } } void UseAsWithNullable(System.ValueType val) { int? j = val as int?; if (j != null) { Console.WriteLine(j); } else { Console.WriteLine("Could not convert " + val.ToString()); } } }
相关文章推荐
- 【转】 测试职业思考:如何成为一名优秀的软件测试工程师
- Angularjs 自定义服务 provide 里 provider 方法 以及 factory、 service 方法以及 provider 供应商的概念
- 关于---内网IP和外网IP
- [python]pygame游戏开发学习(二)
- COleDateTime SYSTEMTIME CTime
- android studio Cannot reload AVD list问题
- QPID例子二(DEMO)
- 深入浅出RxJava(一:基础篇)
- Xcode快捷键
- 使用maven运行Java main的2种方式
- Java-URL类详解
- WampSever 远程访问出现Forbidden You don’t have permission to access / on this server.解决方法
- hdu1978 记忆化搜索
- 【05】emmet系列之各种缩写
- 详解Java中多进程编程的实现
- Oralce drop all tables
- ActiveDirectory之忘记管理员密码时的重置方法
- Android适配之适配不同的系统版本
- java同步代码块 实现的买票功能
- Express重定向