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()); } }
Language Basics
Define an alias to represent a namespace
// 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"); } }
Using the alias keyword to refer to a nested namespace
// 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
/* 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
/*
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
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
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); } }