zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Chrome 中的彩蛋,一款小游戏,你知道吗?
    Json对象与Json字符串互转(4种转换方式)
    [PHP自动化-进阶]005.Snoopy采集框架介绍
    [PHP自动化-进阶]004.Snoopy VS CURL 模拟Discuz.net登陆
    [PHP自动化-进阶]003.CURL处理Https请求访问
    [PHP自动化-进阶]002.CURL模拟登录带有验证码的网站
    [PHP自动化-进阶]001.CURL模拟登录并采集数据
    [注]2015中国程序员生存报告,你苦你先看@^@
    [JavaWeb基础] 016.Struts2 国际化配置
    [工具推荐]_iOS音频批量转换
  • 原文地址:https://www.cnblogs.com/start-from-scratch/p/13755435.html
Copyright © 2011-2022 走看看