• C# is运算符的作用(转)


    原文链接:https://www.cnblogs.com/cheny3636/p/4995784.html

    is运算符的含义:检查对象是不是给定类型,或者是否可以转换为给定类型,如果是,这个运算符就返回True.

    is运算符的语法:<operand> is <type>

    这个表达式的结果如下:如果<type>是一个类类型,而<operand>也是该类型,或者它继承了该类型,或者它可以封箱到该类型中,则结果返回True.

                                 如果<type>是一个接口类型,而<operand>也是该类型,或者它是实现该接口的类型,则结果返回True.

               如果<type>是一个值类型,而<operand>也是该类型,或者它可以拆箱到该类型中,则返回True.

    is实例:

     1 namespace Chapter11
     2 {
     3     class Checker
     4     {
     5         public void Check(object param1)
     6         {
     7             if (param1 is ClassA)
     8                 Console.WriteLine("Variable can be converted to ClassA");
     9             else
    10                 Console.WriteLine("Variable can't be converted to ClassA.");
    11 
    12             if (param1 is IMyInterface)
    13                 Console.WriteLine("Variable can be converted to IMyInterface.");
    14             else
    15                 Console.WriteLine("Variable can't converted to IMyInterface.");
    16 
    17             if (param1 is MyStruct)
    18                 Console.WriteLine("Variable can be converted to MyStruct.");
    19             else
    20                 Console.WriteLine("Variable can't be converted to MyStruct.");
    21         }
    22     }
    23     interface IMyInterface
    24     { }
    25     class ClassA : IMyInterface
    26     { }
    27     class ClassB :IMyInterface
    28     { }
    29 
    30     class ClassC
    31     { }
    32 
    33     class classD : ClassA
    34     { }
    35 
    36     struct MyStruct : IMyInterface
    37     { }
    38     class Program
    39     {
    40         static void Main(string[] args)
    41         {
    42             Checker check = new Checker();
    43             ClassA try1 = new ClassA();
    44             ClassB try2 = new ClassB();
    45             ClassC try3 = new ClassC();
    46             classD try4 = new classD();
    47             MyStruct try5 = new MyStruct();
    48             object try6 = try5;
    49             Console.WriteLine("Analyzing ClassA type variable:");
    50             check.Check(try1);
    51             Console.WriteLine("Analyzing ClassB type variable:");
    52             check.Check(try2);
    53             Console.WriteLine("Analyzing ClassC type variable:");
    54             check.Check(try3);
    55             Console.WriteLine("Analyzing ClassD type variable:");
    56             check.Check(try4);
    57             Console.WriteLine("Analyzing MyStruct type variable:");
    58             check.Check(try5);
    59             Console.WriteLine("Analyzing boxed MyStruct type variable:");
    60             check.Check(try6);
    61             Console.ReadKey();
    62         }
    63     }
    64 }
  • 相关阅读:
    js的基本数据类型有哪些?
    UML 类图
    三种代理模式
    jsp 知识点
    httpServlet
    Qt时间&日期
    Microsoft visual studio C 运行时库 在 xx.exe中检测到一个错误
    C++调用COM之错
    PCL中的bug修改
    Qt使用SQLite
  • 原文地址:https://www.cnblogs.com/start-from-scratch/p/13755435.html
Copyright © 2020-2023  润新知