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(); 
  } 
}
           
          


Namespaces prevent name conflicts

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Namespaces prevent name conflicts. 
  
using System;  
  
// 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;  
    }  
  }  
}  
 
// Declare another namespace.  
namespace Counter2 {  
  /* This CountDown is in the default namespace and  
     does not conflict with the one in Counter. */ 
  class CountDown { 
    public void count() { 
      Console.WriteLine("This is count() in the " + 
                        "Counter2 namespace."); 
    } 
  } 
} 
 
public class NSDemo2 {  
  public static void Main() {  
    // This is CountDown in the Counter namespace. 
    Counter.CountDown cd1 = new Counter.CountDown(10);  
 
    // This is CountDown in the default namespace. 
    Counter2.CountDown cd2 = new Counter2.CountDown(); 
 
    int i;  
  
    do {  
      i = cd1.count();  
      Console.Write(i + " ");  
    } while(i > 0);  
    Console.WriteLine();  
  
    cd2.count(); 
  }  
}

           
          


Demonstrate a namespace

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

using System;
// Demonstrate a namespace. 
// 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 NSDemo1 { 
  public static void Main() { 
    Counter.CountDown cd1 = new Counter.CountDown(10); 
    int i; 
 
    do { 
      i = cd1.count(); 
      Console.Write(i + " "); 
    } while(i > 0); 
    Console.WriteLine(); 
 
    Counter.CountDown cd2 = new Counter.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(); 
  } 
} 


           
          


Demonstrates using as a statement

image_pdfimage_print

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Using.cs — Demonstrates using as a statement
//
// Compile this program with the following command line:
// C:>csc Using.cs
using System;
using System.Windows.Forms;
using System.Drawing;
using Pen = System.Drawing.Pen;
using PaintHandler = System.Windows.Forms.PaintEventHandler;

namespace nsForm
{
public class UsingForm : Form
{
public UsingForm ()
{
this.Text = “Using Statement”;
this.Paint += new PaintHandler(this.OnPaint);
}
static public void Main ()
{
Application.Run(new UsingForm());
}
private Color [] clr = new Color []
{
Color.Red,
Color.Green,
Color.Blue
};

private void OnPaint (object obj, PaintEventArgs e)
{
Rectangle client = this.ClientRectangle;
int side = (client.Right – client.Left) / 3;
for (int x = 0; x < 3; ++x) { using (Pen pen = new Pen(clr[x], (float) 2.0)) { client = Rectangle.Inflate (client, -10, -10); e.Graphics.DrawEllipse (pen, client); } } } } } [/csharp]