C#的is关键字
来源:互联网 发布:st单片机 编辑:程序博客网 时间:2024/05/16 14:17
Checks if an object is compatible with a given type. For example, it can be determined if an object is compatible with the string type like this:
if (obj is string){}
An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown. For more information, see 7.6.6 Cast expressions.
The is keyword results in a compile-time warning if the expression is known to always be true or to always be false, but typically evaluates type compatibility at run time.
The is operator cannot be overloaded.
Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered.
// cs_keyword_is.cs// The is operator.using System;class Class1{}class Class2{}class IsTest{ static void Test(object o) { Class1 a; Class2 b; if (o is Class1) { Console.WriteLine("o is Class1"); a = (Class1)o; // Do something with "a." } else if (o is Class2) { Console.WriteLine("o is Class2"); b = (Class2)o; // Do something with "b." } else { Console.WriteLine("o is neither Class1 nor Class2."); } } static void Main() { Class1 c1 = new Class1(); Class2 c2 = new Class2(); Test(c1); Test(c2); Test("a string"); }}
Output
o is Class1o is Class2o is neither Class1 nor Class2.
- C#的is关键字
- C#关键字 - is 和 as
- C#中的is,as关键字
- C# is和as关键字
- c# 中is和as关键字的区别
- c# 中 is 和 as 关键字的区别
- C#中as和is关键字的作用
- 如何:使用 C++ 实现 C# 的 is 和 as 关键字
- c# 中 is 和 as 关键字的区别
- C#中AS和IS关键字的用法
- C#中is,as,using关键字的使用
- c#中is关键字与as关键字的区别与使用
- C#中as和is关键字作用
- C#中的关键字is和as
- GetType与C#关键字typeof ,is,as
- C#的关键字 [STAThread]
- C#的关键字 [STAThread]
- C#的override关键字
- android webclient使用
- Using Repository and Unit of Work patterns with Entity Framework 4.0
- 0.asm-call与jmp机器码取地址计算
- 任务和返回堆 - Tasks and Back Stack
- System.map文件的作用
- C#的is关键字
- Java线程:创建与启动
- Perl 内部结构详解 -- PerlGuts Illustrated
- 服务 - Services
- web作用域
- 苹果跟Amazon越来越像,两巨头战事升级
- Loongson重启的时候不显示BootMenu
- web service 原理
- android Bundle传值