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]

Deal with the arguments

image_pdfimage_print
   
 
using System;
class SayHello {
    public static void Main(string[] args) {
        if (args.Length > 0) {
            foreach (string arg in args) {
                if (arg.Equals("/help"))
                    Console.WriteLine("Run this program as follows: sayhello.exe [name1] ");
                else
                    Console.WriteLine("Hello " + "{0}", arg);
            }
        } else
            Console.WriteLine("For help, run sayhello.exe /help");

    }
}

    


Return different value to the operating system based on the argument length

image_pdfimage_print
   
 

using System;
class SayHello {
    public static int Main(string[] args) {
        if (args.Length > 0) {
            foreach (string arg in args) {
                if (arg.Equals("/help")) {
                    Console.WriteLine("Run this program as follows:" +
                              "sayhello.exe [name1] ");
                    return (1);
                } else
                    Console.WriteLine("Hello " + "{0}", arg);
            }
            return (0);
        } else
            Console.WriteLine("For help, run sayhello.exe /help");
        return (2);
    }
}

    


Using command-line arguments to initialize an array.

image_pdfimage_print

using System;

public class InitArray
{
public static void Main( string[] args )
{
int arrayLength = Convert.ToInt32( args[ 0 ] );
int[] array = new int[ arrayLength ]; // create array

int initialValue = Convert.ToInt32( args[ 1 ] );
int increment = Convert.ToInt32( args[ 2 ] );

for ( int counter = 0; counter < array.Length; counter++ ) array[ counter ] = initialValue + increment * counter; Console.WriteLine( "{0}{1,8}", "Index", "Value" ); for ( int counter = 0; counter < array.Length; counter++ ) Console.WriteLine( "{0,5}{1,8}", counter, array[ counter ] ); } } [/csharp]