文档库 最新最全的文档下载
当前位置:文档库 › 第一章 C#概述

第一章 C#概述

第一章 C#语言介绍

1、写一个 Hello,World 程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//以上代码为必要引用的命名空间

namespace ConsoleApplication //本程序的命名空间
{
class Program
{
static void Main(string[] args) //C# main 主程序,确保唯一性
{
Console.WriteLine("Hello World!!");


Console.ReadKey(); //作用是让控制台暂停,以便查看显示的数据
}
}
}


2、C#的程序结构

using System; //引入命名空间
namespace YourNamespace //命名空间
{
class YourClass //类
{
}

struct YourStruct //结构
{
}

interface IYourInterface //接口
{
}

delegate int YourDelegate(); //委托

enum YourEnum //枚举
{
}

namespace YourNestedNamespace //嵌套的命名空间
{
struct YourStruct //结构
{
}
}

class YourMainClass //类
{
static void Main(string[] args) //main 主程序
{
//Your program starts here... //程序体
}
}
}

【例】在CSharpBook.Chapter01的命名空间中声明一个名为 Stack 的类。Stack 实现FILO(先进后出)的堆栈功能。

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

上面的CSharpBook.Chapter01命名空间不存 Main 主入口,所以我们需要建一个 Main 入口去使用 CSharpBook.Chapter01 命名空间

using System;
using CSharpBook.Chapter01; //引用上面建好的命名空间

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args) //Main入口
{

Stack s = new Stack();
s.Push(1); //1进栈
s.Push(10); //10进栈
s.Push(100); //100进栈

Console.WriteLine(s.Pop()); //出栈
Console.WriteLine(s.Pop()); //出栈
Console.WriteLine(s.Pop()); //出栈


Console.ReadKey(); //作用是让控制台暂停,以便查看显示的数据
}
}
}

3、命名空间
问:什么是命名空间?
答:命名空间与文件或组件不同,它是一种逻辑组合,在C#文件中定义类时,可以把它包

括在命名空间定义中。C#程序中类型由指示逻辑层次结构的完全限定名(fully qualified name)描述。例如,CSharpBook.Chapter01.HelloWorld 表示 CShaprBook 命名空间的子命名空间 Chapter01 中的 HelloWorld 类。

①定义命名空间
namespace 命名空间名称;

命名空间名称格式一般如下:
.(|)|[.][.]

一个源程序文件中可以包含多个命名空间;同一命名空间可以在多个源程序文件中定义。命名空间可以嵌套。同一命名空间中不允许定义重名的类型。

如果源代码中没有指定 namespace,则使用默认命名空间。除非简单的小程序,一般不推荐使用默认命名空间。

②访问命名空间
要访问命名空间中的类型,可以通过如下的完全限定方式访问:
[.].类型
【例】 System.Console.WriteLine("Hello,World!");

【例】命名空间和类型声明及其关联的完全限定名示例。
class A{} //A 默认命名空间
namespace X //X
{
class B //X.B
{
class C{} //X.B.C
}
namespace Y //X.Y
{
class D{} //X.Y.D
}
}
namespace X.Y //X.Y
{
class E{} //X.Y.E
}

如是应用程序频繁使用某命名空间,为了避免程序员在每次使用其中包含的方法时都要指定完全限定的名称,可以在C#应用程序开始时用 using 指定引用该命名空间,以通过非限定方式直接引用该命名空间中的类型。
如控制台显示 Hello,World!:
System.Console.WriteLine("Hello,World!");
引用 System 后,
using System;
Console.WriteLine("Hello,World!");

③命名空间别名
using 指定还用于创建命名空间的别名,别名用于提供引用特定命名空间的简写方法。使用 using 指令指定命名空间或类型的别名的格式如下:using 别名=命名空间或类型名;
如果别名指向命名空间,则使用“别名::类型”的形式进行调用;如果别名指向类型名,则使用 “别名.方法”进行调用。
【例】
using AliasNS=System;
using AliasClass=Syste.Console;
namespace CSharpBook.Chapter01
{
class AliasNSTest
{
static void Main()
{
//错误!
//AliasNS.Console.WriteLine("Hi 1");
AliasNS:Console.WriteLine("Hi 2"); //OK
//错误!
//AliasClass::WriteLine("Hi 3");
AliasClass.WriteLine("Hi 4"); //OK
}
}
}


④全局命名空间
当成员可能被同名的其它实体隐藏时,可以使用全局命名空间来访问正确的命名空间中的类型。C#程序中,如果使用全局命名空间限定符 global :: ,则对其右侧标识符的搜索将从全局命名空间开始。
【例】
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.WriteLine(number);
//访问正确的命名空间中的类型
global::System.Console.WriteLine(number);
}
}
}

⑤命名空间举例
演示在两个不同的命名空间中分别定义名称相同的类(SampleClass)

using System;

namespace ConsoleApplication
{
class SampleClass
{
public void SampleMethod()
{
Console.WriteLine("SampleMethod inside CSharpBook.Chapter01");
}
}
namespace NestedNamespace //创建嵌套的命名空间
{
class SampleClass
{
public void SampleMethod()
{
Console.WriteLine("SampleMethod inside CSharpBook.Chapter01.NestedNamespace");
}
}
}


class Program
{
static void Main()
{
//显示“SampleMethod inside CSharpBook.Chapter01”
SampleClass outer = new SampleClass();
outer.SampleMethod();
//显示“SampleMethod inside CSharpBook.Chapter01”
ConsoleApplication.SampleClass outer2 = new ConsoleApplication.SampleClass();
outer2.SampleMethod();
//显示 “SampleMethod inside”
//ConsoleApplication.NestedNamespace;"
NestedNamespace.SampleClass inner = new NestedNamespace.SampleClass();
inner.SampleMethod();
//显示 “SampleMethod inside”
//ConsoleApplication.NestedNamespace
ConsoleApplication.NestedNamespace.SampleClass inner2 = new NestedNamespace.SampleClass();
inner2.SampleMethod();


Console.ReadKey(); //作用是让控制台暂停,以便查看显示的数据
}

}
}

⑥类和对象
C#程序主要由 .NET Framework 类库中定义的类型和用户自定义类型组成。C#类型包含类、结构、接口、枚举和委托等。类(class)是最基础的C#类型。类是一个数据结构,将状态(字段)和操作(方法和其他函数成员)组合在一个单元中。可以使用 new 运算符创建类的实例对象,通过调用对象的方法进行各种操作,实现应用程序的不同功能。
【例】 类和对象示例
using System;

namespace ConsoleApplication
{
public class Point //定义平面点坐标
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Program
{
static void Main(string[] args)
{
Point P1 = new Point(0, 0); //点1
Point P2 = new Point(10, 20); //点2
Console.WriteLine("两个点的坐标分别为:");
Console.WriteLine("p1:x="+P1.x+",y="+P1.y);
Console.WriteLine("p2:x=" + P2.x + ",y=" + P2.y);

Console.ReadKey(); //作用是让控制台暂停,以便查看显示的数据
}
}
}

4、Main方法
C#的可执行程序必须包含一个Main方法,用于控制程序的开始和结束。
在编译C#

控制台或Windows应用程序时,默认情况下,编译器会在源代码中查找 Main 方法,并使该方法成为程序的入口。如果有多个 Main 方法,编译器就会返回一个错误。
Main方法声明:
①不带参数,返回 void
static void Main()
{
//....
}
②不带参数,返回int
static int Main()
{
//...
return 0;
}
③带参数,返回void
static void Main(string[] args)
{
//...
}
④带参数,返回int
static int Main(string[] args)
{
//...
return 0;
}
⑤命令行参数
Main方法的参数是表示命令行参数的 String 数组。通常通过测试 args.Length 属性来检查参数是否存在,args[0]表示第一个参数,args[1]表示第二个参数,依此类推。
【例】输出命令行参数个数以及各参数内容
using System;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//输出参数个数
Console.WriteLine("参数个数={0}",args.Length);
//使用for语句输出各参数值
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("Arg[{0}]=[{1}]",i,args[i]);
}
//通过foreach语句输出各参数的值
foreach (string s in args)
{
Console.WriteLine(s);
}
}
}
}
使用方法:可执行文件名.exe a b c
结果返回:参数个数 3 ,分别为 a b c

⑥Main返回值
Main方法可以是 void 类型,也可以返回整型值 int 。如果不需要使用 Main 的返回值,则使用 void 可以使代码变简洁。而返回整数可以使程序将状态信息传递给调用该可执行文件的其它程序或脚本文件。
【例】如是不带命令行参数,则给出相应的提示,否则,输出命令行参数信息
using System;

namespace ConsoleApplication
{
class Program
{
public static int Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("请输入一个string作为参数:");
return 1;
}
else
{
Console.WriteLine("Hello," + args[0]);
return 0;
}
}
}
}

使用 bat 批处理文件运行测试效果
rem MainTest.bat
@echo off
ConsoleApplication.exe
@if "%ERRORLEVEL%" == "0" goto good1
:fail1
echo Execution Failed
echo return value=%ERRORLEVEL%
goto end1
:good1
echo Execution Succeded
echo return value =%ERRORLEVEL%
goto end1
:end1
ConsoleApplication.exe zhouzhihua
@if "%ERRORLEVEL%" == "0" goto good0
:fail0
echo Execution Failed
echo return value=%ERRORLEVEL%
goto end0
:good0
echo Execution Succeded
echo return value =%ERRORLEVEL%
goto end0
:end0

5、注释
①C#注释
单行注释 //....
多行注释 /*.......*/
②XML文本档注释
单行注释 ///....
XML注释需要在空白行进行否则不能自动添加相关代码

相关文档