Namespaces can be nested

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Namespaces can be nested. 
 
using System; 
 
namespace NS1 { 
  class ClassA { 
     public ClassA() { 
       Console.WriteLine("constructing ClassA"); 
    } 
  } 
  namespace NS2 { // a nested namespace 
    class ClassB { 
       public ClassB() { 
         Console.WriteLine("constructing ClassB"); 
      } 
    } 
  } 
} 
 
public class NestedNSDemo { 
  public static void Main() { 
    NS1.ClassA a= new NS1.ClassA(); 
 
 // NS2.ClassB b = new NS2.ClassB(); // Error!!! NS2 is not in view 
 
    NS1.NS2.ClassB b = new NS1.NS2.ClassB(); // this is right 
  } 
}

           
          


Namespaces are additive

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

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

using System;

// Bring Counter into view.
using Counter;

// Here is one Counter namespace.
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;
}
}
}

// Here is another Counter namespace.
namespace Counter {
// A simple count-up counter.
class CountUp {
int val;
int target;

public int Target {
get{
return target;
}
}

public CountUp(int n) {
target = n;
val = 0;
}

public void reset(int n) {
target = n;
val = 0;
}

public int count() {
if(val < target) return val++; else return target; } } } public class NSDemo5 { public static void Main() { CountDown cd = new CountDown(10); CountUp cu = new CountUp(8); int i; do { i = cd.count(); Console.Write(i + " "); } while(i > 0);
Console.WriteLine();

do {
i = cu.count();
Console.Write(i + ” “);
} while(i < cu.Target); } } [/csharp]

Creating an alias

image_pdfimage_print
   


using System;
using MyAlias=MyNamespace.Nested.MyClass;

namespace MyNamespace {
  namespace Nested {
       namespace MyClass {
             public class Foo
             {
                  int _Value;
                  public Foo() {
                    _Value = 0;
                  }
                  public int getValue()
                  {
                    return _Value;
                  }
             }
       }
  }
}


class Test {
  public static void Main() {
      MyAlias.Foo myFoo = new MyAlias.Foo();
      Console.WriteLine("Value {0}", myFoo.getValue());
  }
}

           
          


Define an alias to represent a namespace

image_pdfimage_print
   

// 
using CmpDb = YourCompany.SecondPartNamespace.InnerNamespace.YourClass;

namespace YourCompany.SecondPartNamespace {
  namespace InnerNamespace {
    public class YourClass {
      public static void Open( string tblName ) {
         System.Console.WriteLine("{0}", tblName );
      }
    }
  }
}

public class CH1_14 {
  public static void Main() {
    CmpDb.Open("fred");
  }
}


           
          


Demonstrate the % operator

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the % operator. 
 
using System; 
 
public class ModDemo {    
  public static void Main() {    
    int iresult, irem; 
    double dresult, drem; 
 
    iresult = 10 / 3; 
    irem = 10 % 3; 
 
    dresult = 10.0 / 3.0; 
    drem = 10.0 % 3.0;  
 
    Console.WriteLine("Result and remainder of 10 / 3: " + 
                       iresult + " " + irem); 
    Console.WriteLine("Result and remainder of 10.0 / 3.0: " + 
                       dresult + " " + drem); 
  }    
}


           
          


Mod test

image_pdfimage_print

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class ModTester
{

public static int Main()
{
for (int counter=1; counter<=100; counter++) { Console.Write("{0} ", counter); if ( counter % 10 == 0 ) { Console.WriteLine(" {0}", counter); } } return 0; } } [/csharp]