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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.