/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate as. using System; class A {} class B : A {} public class CheckCast1 { public static void Main() { A a = new A(); B b = new B(); b = a as B; // cast, if possible if(b==null) Console.WriteLine("Cast b = (B) a is NOT allowed."); else Console.WriteLine("Cast b = (B) a is allowed"); } }
Language Basics
Use is to avoid an invalid cast
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use is to avoid an invalid cast. using System; class A {} class B : A {} public class CheckCast { public static void Main() { A a = new A(); B b = new B(); // Check to see if a can be cast to B. if(a is B) // if so, do the cast b = (B) a; else // if not, skip the cast b = null; if(b==null) Console.WriteLine("Cast b = (B) a is NOT allowed."); else Console.WriteLine("Cast b = (B) a is allowed"); } }
Demonstrate is
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate is. using System; class A {} class B : A {} public class UseIs { public static void Main() { A a = new A(); B b = new B(); if(a is A) Console.WriteLine("a is an A"); if(b is A) Console.WriteLine("b is an A because it is derived from A"); if(a is B) Console.WriteLine("This won't display -- a not derived from B"); if(b is B) Console.WriteLine("B is a B"); if(a is object) Console.WriteLine("a is an Object"); } }
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(" Calling 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( " Treating the object as Compressible... "); icDoc.Compress(); } else { Console.WriteLine("Could not cast to ICompressible"); } } [STAThread] static void Main() { TesterISAS t = new TesterISAS(); t.Run(); } } }
Illustrates the use of the is operator
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example3_9.cs illustrates the use of the is operator */ public class Example3_9 { public static void Main() { int myInt = 0; bool compatible = myInt is int; System.Console.WriteLine("myInt is int = " + compatible); compatible = myInt is long; System.Console.WriteLine("myInt is long = " + compatible); compatible = myInt is float; System.Console.WriteLine("myInt is float = " + compatible); } }
Bitwise Operators 2
/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_2___Operators_and_Excpressions { public class BitwiseOperators { static void Main(string[] args) { long MyBit = 0x1; long MyBitResult = 0; MyBitResult = MyBit & 0x1; MyBitResult = MyBit | 0x2; MyBitResult = MyBit ^ 0x4; } } }
A class that displays the binary representation of a value
/* C# A Beginner's Guide By Schildt Publisher: Osborne McGraw-Hill ISBN: 0072133295 */ /* Project 5-3 A class that displays the binary representation of a value. */ using System; class ShowBits { public int numbits; public ShowBits(int n) { numbits = n; } public void show(ulong val) { ulong mask = 1; // left-shift a 1 into the proper position mask <<= numbits-1; int spacer = 0; for(; mask != 0; mask >>= 1) { if((val & mask) != 0) Console.Write("1"); else Console.Write("0"); spacer++; if((spacer % 8) == 0) { Console.Write(" "); spacer = 0; } } Console.WriteLine(); } } // Demonstrate ShowBits. public class ShowBitsDemo { public static void Main() { ShowBits b = new ShowBits(8); ShowBits i = new ShowBits(32); ShowBits li = new ShowBits(64); Console.WriteLine("123 in binary: "); b.show(123); Console.WriteLine(" 87987 in binary: "); i.show(87987); Console.WriteLine(" 237658768 in binary: "); li.show(237658768); // you can also show low-order bits of any integer Console.WriteLine(" Low order 8 bits of 87987 in binary: "); b.show(87987); } }