Demonstrate the short-circuit operators


   
 
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the short-circuit operators. 
 
using System; 
 
public class SCops {    
  public static void Main() {    
    int n, d; 
 
    n = 10; 
    d = 2; 
    if(d != 0 && (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n); 
 
    d = 0; // now, set d to zero 
 
    // Since d is zero, the second operand is not evaluated. 
    if(d != 0 && (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n);  
     
    /* Now, try the same thing without short-circuit operator. 
       This will cause a divide-by-zero error.  */ 
    if(d != 0 & (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n); 
  }    
}


           
         
     


Demonstrate the relational and logical operators


   
 
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the relational and logical operators. 
 
using System; 
 
public class RelLogOps {    
  public static void Main() {    
    int i, j; 
    bool b1, b2; 
 
    i = 10; 
    j = 11; 
    if(i < j) Console.WriteLine("i < j"); 
    if(i <= j) Console.WriteLine("i <= j"); 
    if(i != j) Console.WriteLine("i != j"); 
    if(i == j) Console.WriteLine("this won&#039;t execute"); 
    if(i >= j) Console.WriteLine("this won&#039;t execute"); 
    if(i > j) Console.WriteLine("this won&#039;t execute"); 
 
    b1 = true; 
    b2 = false; 
    if(b1 &amp; b2) Console.WriteLine("this won&#039;t execute"); 
    if(!(b1 &amp; b2)) Console.WriteLine("!(b1 &amp; b2) is true"); 
    if(b1 | b2) Console.WriteLine("b1 | b2 is true"); 
    if(b1 ^ b2) Console.WriteLine("b1 ^ b2 is true"); 
  }    
}

           
         
     


Demonstrate the difference between prefix postfix forms of ++

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*
Demonstrate the difference between prefix
postfix forms of ++.
*/
using System;

public class PrePostDemo {
public static void Main() {
int x, y;
int i;

x = 1;
Console.WriteLine(“Series generated using y = x + x++;”);
for(i = 0; i < 10; i++) { y = x + x++; // postfix ++ Console.WriteLine(y + " "); } Console.WriteLine(); x = 1; Console.WriteLine("Series generated using y = x + ++x;"); for(i = 0; i < 10; i++) { y = x + ++x; // prefix ++ Console.WriteLine(y + " "); } Console.WriteLine(); } } [/csharp]

Interfaces:The As Operator


   


/*
A Programmer&#039;s Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 10 - InterfacesThe As Operator
// copyright 2000 Eric Gunnerson
using System;

public class TheAsOperator
{
    public static void Main()
    {
        DiagramObject[] dArray = new DiagramObject[100];
        
        dArray[0] = new DiagramObject();
        dArray[1] = new TextObject("Text Dude");
        dArray[2] = new TextObject("Text Backup");
        
        // array gets initialized here, with classes that
        // derive from DiagramObject. Some of them implement
        // IScalable.
        
        foreach (DiagramObject d in dArray)
        {
            IScalable scalable = d as IScalable;
            if (scalable != null)
            {
                scalable.ScaleX(0.1F);
                scalable.ScaleY(10.0F);
            }
        }
    }
}

interface IScalable
{
    void ScaleX(float factor);
    void ScaleY(float factor);
}
public class DiagramObject
{
    public DiagramObject() {}
}
public class TextObject: DiagramObject, IScalable
{
    public TextObject(string text)
    {
        this.text = text;
    }
    // implementing IScalable.ScaleX()
    public void ScaleX(float factor)
    {
        Console.WriteLine("ScaleX: {0} {1}", text, factor);
        // scale the object here.
    }
    
    // implementing IScalable.ScaleY()
    public void ScaleY(float factor)
    {
        Console.WriteLine("ScaleY: {0} {1}", text, factor);
        // scale the object here.
    }
    
    private string text;
}


           
          


Operators and Expressions:Type operators:Is

   


/*
A Programmer&#039;s Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 14 - Operators and ExpressionsType operatorsIs
// copyright 2000 Eric Gunnerson
using System;
interface IAnnoy
{
    void PokeSister(string name);
}
class Brother: IAnnoy
{
    public void PokeSister(string name)
    {
        Console.WriteLine("Poking {0}", name);
    }
}
class BabyBrother
{
}

public class TypeoperatorsIs
{
    public static void AnnoyHer(string sister, params object[] annoyers)
    {
        foreach (object o in annoyers)
        {
            if (o is IAnnoy)
            {
                IAnnoy annoyer = (IAnnoy) o;
                annoyer.PokeSister(sister);
            }
        }
    }
    public static void Main()
    {
        TestoperatorsIs.AnnoyHer("Jane", new Brother(), new BabyBrother());
    }
}