C# as 运算符

来源:互联网 发布:怎么查看手机端口 编辑:程序博客网 时间:2024/05/08 02:51

as 运算符类似于强制转换操作。 但是,因此,如果转换是不可能的,as 返回 null 而不引发异常,

请注意 as 运算符执行只引用转换、nullable 转换和装箱转换。 as 运算符不能执行其他转换,如用户定义的转换,应是通过使用转换的表达式。

class csrefKeywordsOperators   {       class Base       {           public override string  ToString()           {             return "Base";           }       }       class Derived : Base        { }       class Program       {           static void Main()           {               Derived d = new Derived();               Base b = d as Base;               if (b != null)               {                   Console.WriteLine(b.ToString());               }           }       }   }

0 0
原创粉丝点击