Test is and as : Operator is as « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Language Basics » Operator is asScreenshots 
Test is and as
Test is and as

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/
 using System;

 namespace InterfaceDemo
 {
     interface IStorable
     {
         void Read();
         void Write(object obj);
         int Status get; set; }

     }

     // the Compressible interface is now the
     // base for ILoggedCompressible
     interface ICompressible
     {
         void Compress();
         void Decompress();
     }

     // extend ICompressible to log the bytes saved
     interface ILoggedCompressible : ICompressible
     {
         void LogSavedBytes();
     }


     // Document implements both interfaces
     class Document : IStorable, ILoggedCompressible
     {
         // the document constructor
         public Document(string s)
         {
             Console.WriteLine("Creating document with: {0}", s);

         }

         // implement IStorable
         public void Read()
         {
             Console.WriteLine(
                 "Implementing the Read Method for IStorable");
         }

         public void Write(object o)
         {
             Console.WriteLine(
                 "Implementing the Write Method for IStorable");
         }

         public int Status
         {
             get return status; }
             set status = value; }
         }

         // implement ICompressible
         public void Compress()
         {
             Console.WriteLine("Implementing Compress");
         }

         public void Decompress()
         {
             Console.WriteLine("Implementing Decompress");
         }

         // implement ILoggedCompressible
         public void LogSavedBytes()
         {
             Console.WriteLine("Implementing LogSavedBytes");
         }

         // hold the data for IStorable's Status property
         private int status = 0;
     }


    public class TesterISAS
    {
       public void Run()
       {
           Document doc = new Document("Test Document");

           // cast using as, then test for null
           IStorable isDoc = doc as IStorable;
           if (isDoc != null)
           {
               isDoc.Read();
           }
           else
           {
               Console.WriteLine("Could not cast to IStorable");
           }



           ILoggedCompressible ilDoc = doc as ILoggedCompressible;
           if (ilDoc != null)
           {
               Console.Write("\nCalling both ICompressible and ");
               Console.WriteLine("ILoggedCompressible methods...");
               ilDoc.Compress();
               ilDoc.LogSavedBytes();
           }
           else
           {
               Console.WriteLine("Could not cast to ILoggedCompressible");
           }

           // cast using as, then test for null
           ICompressible icDoc = doc as ICompressible;
           if (icDoc != null)
           {
               Console.WriteLine(
                   "\nTreating the object as Compressible... ");
               icDoc.Compress();
           }
           else
           {
               Console.WriteLine("Could not cast to ICompressible");
           }
       }

       [STAThread]
       static void Main()
       {
          TesterISAS t = new TesterISAS();
          t.Run();
       }
    }
 }

           
       
Related examples in the same category
1.Illustrates the use of the is operatorIllustrates the use of the is operator
2.Demonstrate isDemonstrate is
3.Use is to avoid an invalid castUse is to avoid an invalid cast
4.Demonstrate asDemonstrate as
5.Operators and Expressions:Type operators:Is
6.Interfaces:The As OperatorInterfaces:The As Operator
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.