Using the is Keyword to Work with an Interface

image_pdfimage_print
   
 


using System;
   
public interface IPrintMessage
{
    void Print();
};
   
class Class1
{
    public void Print()
    {
        Console.WriteLine("Hello from Class1!");
    }
}
   
class Class2 : IPrintMessage
{
    public void Print()
    {
        Console.WriteLine("Hello from Class2!");
    }
}
   
class MainClass
{
    public static void Main()
    {
        PrintClass   PrintObject = new PrintClass();
   
        PrintObject.PrintMessages();
    }
}
   
class PrintClass
{
    public void PrintMessages()
    {
        Class1      Object1 = new Class1();
        Class2      Object2 = new Class2();
   
        PrintMessageFromObject(Object1);
        PrintMessageFromObject(Object2);
    }
   
    private void PrintMessageFromObject(object obj)
    {
        if(obj is IPrintMessage)
        {
            IPrintMessage PrintMessage;
   
            PrintMessage = (IPrintMessage)obj;
            PrintMessage.Print();
        }
    }
}

    


Use internal

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use internal. 
 
using System; 
 
class InternalTest { 
  internal int x; 
} 
 
public class InternalDemo { 
  public static void Main() { 
    InternalTest ob = new InternalTest(); 
 
    ob.x = 10; // can access -- in same file 
 
    Console.WriteLine("Here is ob.x: " + ob.x); 
 
  } 
}


           
          


Logical operators with an if statement

image_pdfimage_print

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example4_4.cs illustrates the use of
  logical operators with an if statement
*/

public class Example4_4
{

  public static void Main()
  {

    int reactorTemp = 1500;
    string emergencyValve = "closed";

    if ((reactorTemp > 1000) && (emergencyValve == "closed"))
    {
      System.Console.WriteLine("Reactor meltdown in progress!");
    }

  }

}


           
          


illustrates the use of a nested if statement

image_pdfimage_print

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example4_3.cs illustrates the use of
a nested if statement
*/

public class Example4_3
{

public static void Main()
{

int reactorTemp = 1500;
string emergencyValve = ” “;

if (reactorTemp < 1000) { System.Console.WriteLine("Reactor temperature normal"); } else { System.Console.WriteLine("Reactor temperature too high!"); if (emergencyValve == "closed") { System.Console.WriteLine("Reactor meltdown in progress!"); } } } } [/csharp]

Illustrates the use of an if statement that executes a block

image_pdfimage_print

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example4_2.cs illustrates the use of an if statement
that executes a block
*/

public class Example4_2
{

public static void Main()
{

int smallNumber = 5;
int bigNumber = 100;

if (bigNumber < smallNumber) { System.Console.Write(bigNumber); System.Console.Write(" is less than "); System.Console.Write(smallNumber); } else { System.Console.Write(smallNumber); System.Console.Write(" is less than "); System.Console.Write(bigNumber); } } } [/csharp]

Illustrates the use of the if statement

image_pdfimage_print

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example4_1.cs illustrates the use of the if statement
*/

public class Example4_1
{

  public static void Main()
  {

    int smallNumber = 5;
    int bigNumber = 100;

    if (bigNumber > smallNumber)
      System.Console.WriteLine(bigNumber + " is greater than " +
        smallNumber);
    else
      System.Console.WriteLine(bigNumber + " is less than " +
        smallNumber);

  }

}


           
          


Another if else

image_pdfimage_print

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class NestIfValues
{
static void Main()
{
int temp = 32;

if (temp <= 32) { Console.WriteLine("Warning! Ice on road!"); if (temp == 32) { Console.WriteLine( "Temp exactly freezing, beware of water."); } else { Console.WriteLine("Watch for black ice! Temp: {0}", temp); } } } } [/csharp]