is操作符和as操作符比较

    技术2022-05-12  1

    is操作符在表达式可以安全地转换为“类型”而不抛出异常,结果值为true。      as操作符则将is和转换操作结合起来,首先测试转换是否合法,如果合法则进行转换。如果不合法,as操作符返回null。使用as操作符不用处理异常。同时可以避免两次转换测试的开销。      下面看看测试代码:         //  接口IStorable         interface IStorable         {             void Read();             void Write(object obj);             int Status { get;set;}         }         //  接口ICompressible         interface ICompressible         {             void Compress();             void DeCompress();         }         //  Document类只继承IStorable         public class Document : IStorable         {             private int status = 0;             public int Status             {                 get { return status; }                 set { status = value; }             }             public Document(string s)             {                 Console.WriteLine("Creating document with :{0}", s);             }             public void Read()             {                 Console.WriteLine("Reading.......");             }             public void Write(object o)             {                 Console.WriteLine("Writing.......");             }         }         //  CompressibleDocument类继承Document,同时实现了IStorable和ICompressible接口         public class CompressibleDocument : Document, ICompressible         {             public CompressibleDocument(string s)                 :                 base(s) { }             public void Compress()             {                 Console.WriteLine("Compressing.......");             }             public void DeCompress()             {                 Console.WriteLine("DeCompress.......");             }         }   开始测试: 先使用as操作符: static void Main(string[] args)         {             Document[] docArray = new Document[2];             docArray[0] = new Document("Teat Document");             docArray[1] = new CompressibleDocument("Test CompressibleDocument");             foreach (Document doc in docArray)             {                 //  报出文档名                 Console.WriteLine("Got: {0}", doc);                 IStorable isDoc = doc as IStorable;                 if (isDoc != null)                 {                     isDoc.Read();                 }                 ICompressible icDoc = doc as ICompressible;                 if (icDoc != null)                 {                     icDoc.Compress();                 }             }             Console.ReadLine();         } 测试结果 :   使用is操作符: static void Main(string[] args)         {             Document[] docArray = new Document[2];             docArray[0] = new Document("Teat Document");             docArray[1] = new CompressibleDocument("Test CompressibleDocument");             foreach (Document doc in docArray)             {                 //  报出文档名                 Console.WriteLine("Got: {0}", doc);                 if (doc is IStorable)                 {                     IStorable isDoc = (IStorable)doc;       //  转换为接口                     isDoc.Read();                 }                 if (doc is ICompressible)                 {                     ICompressible icDoc = (ICompressible)doc;                     icDoc.Compress();                           //Document类不会实现Compress接口                 }             }             Console.ReadLine();         } 输出结果: 综合谈论:       如果我们测试的对象的目的是确定它是否属于所需类型,并且若是的话要立即转换,as操作符效率更好。但有时候我们只是测试并不想立即转换,可能根本就不会 转换,只是在对象实现了接口时,将他加入一个列表中。这个is操作符是更好的选择。

    最新回复(0)