Bit and, or, xor, not operator

image_pdfimage_print
   
 

using System;
class LogOpsApp
{
    public static void display(byte left, byte right, byte ans, string op) {
        string Lstr = null;
        string Rstr = null;
        string Astr = null;
        Lstr = Convert.ToString(left, 2);
        if (0 != right)
            Rstr = Convert.ToString(right, 2);
        else
            Rstr = "--------";
        Astr = Convert.ToString(ans, 2);
   
        Console.WriteLine("	{0,8}
{1}	{2,8}
	{3,8}
",Lstr, op, Rstr, Astr);
    }
   
    static void Main(string[] args)
    {
        byte a, b, c, d, e, f, g;
        a = 255;
        b = 132;
        c = 85;
        byte OneOperand = 0;
   
        d = (byte)(a & b);
        display (a, b, d, "&");
        e = (byte)(d | c);
        display (d, c, e, "|");
        f = (byte)(e ^ a);
        display (e, a, f, "^");
        g = (byte)~f;
        display (f, OneOperand, g, "~");
    }
}

    


Bit operator

image_pdfimage_print
   
 
using System;
class Operators {
    static void Main() {
        int a = 10, b = 15, c = 20, d, e, h, i;
        bool f, g, j = (a == b), k;
        d = (a & b);
        e = (a | b);
        f = (j && (b == c));
        g = (j || (b == c));
        h = (a ^ b);
        i = ~b;
        k = !j;
        Console.WriteLine("{0}", d); //10
        Console.WriteLine("{0}", e); //15
        Console.WriteLine("{0}", f); //False
        Console.WriteLine("{0}", g); //False
        Console.WriteLine("{0}", h); //5
        Console.WriteLine("{0}", i); //-16
        Console.WriteLine("{0}", j); //False
        Console.WriteLine("{0}", k); //True
    }
}

    


null pointer

image_pdfimage_print
   
 
using System;

public class MainClass {
    public static void Main(string[] args) {
        MyObject localObject = new MyObject();

        Console.WriteLine("localObject.n is {0}", localObject.n);

        if (localObject.nextObject == null) {
            Console.WriteLine("localObject.nextObject is null");
        }

    }

    public class MyObject {
        internal int n;
        internal MyObject nextObject;
    }
}

    


Demonstrate a namespace 2

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate a namespace. 
using System; 
// Bring Counter into view. 
using Counter; 
 
// Declare a namespace for counters. 
namespace Counter { 
  // A simple countdown counter. 
  class CountDown { 
    int val; 
 
    public CountDown(int n) { 
      val = n; 
    } 
 
    public void reset(int n) { 
      val = n; 
    } 
 
    public int count() { 
      if(val > 0) return val--; 
      else return 0; 
    } 
  } 
} 
 
public class NSDemo3 { 
  public static void Main() { 
    // now, CountDown can be used directly. 
    CountDown cd1 = new CountDown(10); 
    int i; 
 
    do { 
      i = cd1.count(); 
      Console.Write(i + " "); 
    } while(i > 0); 
    Console.WriteLine(); 
 
    CountDown cd2 = new CountDown(20); 
 
    do { 
      i = cd2.count(); 
      Console.Write(i + " "); 
    } while(i > 0); 
    Console.WriteLine(); 
 
    cd2.reset(4); 
    do { 
      i = cd2.count(); 
      Console.Write(i + " "); 
    } while(i > 0); 
    Console.WriteLine(); 
  } 
}