(1-1)
using System;
namespace CSharpBook.Chapter01
{
class HelloWorld
{
public static int Main(String[] args)
{
if (args.Length == 0)
{
Console.WriteLine("请输入一个string作为参数:");
return 1;
}
else
{
Console.WriteLine("Hello," + args[0]);
return 0;
}
}
}
}
( 1-2-3)
using System;
namespace CSharpBook.Chapter01
{
public class Stack
{
Entry top;
public void Push(object data)
{
top =new Entry(top,data);
}
public object Pop()
{
if(top==null)throw new InvalidOperationException();
object result=top.data;
top=top.next;
return result;
}
class Entry
{
public Entry next;
public object data;
public Entry (Entry next,object data)
{
this.next=next ;
this.data=data;
}
}
static void Main()
{
Stack s = new Stack();
s.Push(1);
s.Push(10);
s.Push(100);
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
}
}
}
(1-5)
using AliasNS = System;
using AliasClass = System.Console;
namespace CharpBook.Chapter01
{
class AliasNsText
{
static void Main()
{
//错误!
//AliasNS.Console.WriteLine("Hi 1");
AliasNS::Console.WriteLine("Hi 2");
//错误!
// AliasClass::WriteaLine("Hi 3");
AliasClass.WriteLine("Hi 4");
}
}
}
(1-6)
using System;
namespace CSharpBook.Chapter01
{
class GlobalNSTest
{
//定义一个名为 ‘System’的新类型,为系统制造麻烦 public class System { } //定义一个名为‘Console'的常量,为
系统制造麻烦 const int Console = 7; const int number = 66; static void Main() { //出错了:访问TestApp.Console,Console已被定义为整型 //Console.WriteLine(number); //访问正确的命名空间的类型 global::System.Console.WriteLine(number); } } }
(1-7)
using System;
using CSharpBook.Chapter01;
namespace CSharpBook.Chapter01
{
class SampleClass
{
public void SampleMethod()
{
Console.WriteLine("SempleMethod inside CSharpBook.Chapter01");
}
}
namespace NestedNamespace
{
class SampleClass
{
public void SamepleMethod()
{
Console.WriteLine("SampleMethod CSharpBook.Chapter01.NestedNamespace");
}
}
}
class Program
{
static void Main()
{
//显示"SampleMethod inside CSharpBook.Chapter01"
SampleClass outer=new SampleClass();
outer.SampleMethod();
inside
//显示"SampleMethod inside CSharpBook.Chapter01"
CSharpBook.Chapter01.SampleClass outer2=
new CSharpBook.Chapter01.SampleClass();
outer2.SampleMethod();
//显示"SampleMethod inside CSharpBook.Chapter01.NestNamespace."
NestedNamespace.SampleClass inner=
new NestedNamespace.SampleClass();
inner.SamepleMethod();
//显示"SampleMethod inside CSharpBook.Chapter01.NestNamespace."
CSharpBook.Chapter01.NestedNamespace.SampleClass inner2=
new CSharpBook.Chapter01.NestedNamespace.SampleClass();
}
}
}
(1-8)
using System;
namespace CSharpBook.Chapter01
{
public class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
class PointTest
{
static void Main()
{
Point p1=new Point(0,0);
Point p2=new Point(10,20);
Console.WriteLine("两个点的坐标分别为:");
Console.WriteLine("p1:x=" + p1.x+",y="+p1.y);
Console.WriteLine("p2:x="+ p2.x+ ",y="+p2.y);
Console.ReadKey();
}
}
}
(1-9)
using System;
namespace CSharpBook.Chapter01
{
class HelloWorld1 { static void Main() { Console.WriteLine("Hello World 1!"); } } class HelloWorld2 { static void Main() { Console.WriteLine("Hello World 2!"); } } }
(1-10)
using System;
namespace CSharpBook.Chapter01
{
class C