在软件开发中,类型转换是不可避免的事情。在许多情况下,需要将一个对象(类型)转换为另一种对象(类型),有时会收到InvalidCastException 。因此,要克服C#提供的此类异常,是运算符。
is运算符用于检查对象的运行时类型是否与给定类型兼容。如果给定对象是同类型的,否则,返回false返回true。对于空对象,它还返回false 。
句法:
expression is type
在这里,表达式将被评估为某种类型的实例。 type是要转换表达式结果的类型的名称。如果表达式不为null,并且可以将表达式计算所得的对象转换为指定的类型,则is运算符将返回true,否则将返回false 。
示例1:在下面的代码中,我们有三个类,即Author,Work和GFG。 GFG是包含Main方法的驱动程序类。类“作者”和“工作”具有数据成员和方法。在Main方法中,使用类的实例调用创建的Author和Work类的对象,并调用这些类的方法。之后,产生bool值bool;用于存储is运算符的返回值。代码行,即result = a是Author;用于检查a(类author的对象)是否为Author类型。作为一个作者是类的实例,它将返回true。但是实例w不是Author类型,这就是为什么它返回false的原因。之后,我们为对象a分配null ,与Author实例相比,结果为false。
// C# program to illustrate the
// use of 'is' operator keyword
using System;
class Author {
// data members
public string name;
public int rank;
// method of Author class
public void details(string n, int r)
{
name = n;
rank = r;
}
}
class Work {
// data members
public int articl_no;
public int improv_no;
// method of Work class
public void totalno(int a, int i)
{
articl_no = a;
improv_no = i;
}
}
// Driver Class
public class GFG {
// Main method
static public void Main()
{
// Creating objects of
// Author and Work class
Author a = new Author();
a.details("Ankita", 5);
Work w = new Work();
w.totalno(80, 50);
bool result;
// Check 'a' is of Author
// type or not
// Using is operator
result = a is Author;
Console.WriteLine("Is a is Author? : {0}", result);
// Check w is of Author type
// using is operator
result = w is Author;
Console.WriteLine("Is w is Author? : {0}", result);
// Take the value of a is null
a = null;
// Check null object
// Using is operator
result = a is Author;
Console.WriteLine("Is a is Author? : {0}", result);
}
}
输出:
Is a is Author? : True
Is w is Author? : False
Is a is Author? : False
示例2:在下面的程序中,我们在is运算符的左侧检查派生类型是否为表达式类型。如果派生,则它将返回true,否则将返回false。
// C# program to illustrate the
// use of is operator keyword
using System;
// taking a class
public class G1 {
}
// taking a class
// derived from G1
public class G2 : G1 {
}
// taking a class
public class G3 {
}
// Driver Class
public class GFG {
// Main Method
public static void Main()
{
// creating an instance
// of class G1
G1 obj1 = new G1();
// creating an instance
// of class G2
G2 obj2 = new G2();
// checking whether 'obj1'
// is of type 'G1'
Console.WriteLine(obj1 is G1);
// checking whether 'obj1' is
// of type Object class
// (Base class for all classes)
Console.WriteLine(obj1 is Object);
// checking whether 'obj2'
// is of type 'G2'
Console.WriteLine(obj2 is G2);
// checking whether 'obj2' is
// of type Object class
// (Base class for all classes)
Console.WriteLine(obj2 is Object);
// checking whether 'obj2'
// is of type 'G1'
// it will return true as G2
// is derived from G1
Console.WriteLine(obj2 is G2);
// checking whether obj1
// is of type G3
// it will return false
Console.WriteLine(obj1 is G3);
// checking whether obj2
// is of type G3
// it will return false
Console.WriteLine(obj2 is G3);
}
}
输出:
True
True
True
True
True
False
False
笔记:
- is运算符关键字仅考虑引用,装箱和拆箱转换。
- is运算符不考虑用户定义的转换或使用隐式和显式定义的转换。对于在编译时已知或由隐式运算符处理的转换, is运算符将为此提供警告。