《Web Service 编程 --用C#.NET 开发网络服务》-北京希望出版社
第二章 C# 基本语法
2.1 如何编译C#语言 2.1.1 中间语言 2.1.2 编译参数
CSC.EXE 把C#程序代码编译成IL文件时有很多开关选项
@ csc @rsp1.rsp rsp.1.rsp的内容为 /target:exe /out:sample.exe sample.cs @用来指定响应文件,响应文件是一种包含了许多编译选项的rsp 后缀的文件,在开始用# 做注释可以指定多个响应文件 如: csc @rsp1.rsp @rsp2.rsp /?,/help 帮助 /addmodule 把模块加入文件中 csc /addmodule:module1.dll;module2.dll my.cs /baseaddress 指定编译成为DLL 文件时,DLL 的载入首地址 csc/baseaddress:0x1111000 /target:library my.cs 可以是十进制,八进制,十六进制 /bugreport 把错误放在一个文件中 如:csc /bugreport:bugs.txt my.cs /checked /codepage /debug 该选项会创建一个.pdb文件 如:csc /dubug+ my.cs /define 可以用定义原文件中没有定义的编译变量 如:csc /define:fine my.cs
/doc 会把原代码中的注释编译成为一个XML 文件 csc /doc:my.xml my.cs /fullpaths /incremental /linkresource /main 指定MAIN 的入口 如:csc my1.cs my3.cs /main:my1 /nologo /nooutput 不输出任何文件,但是可以看到编译信息 如:csc /nooutput my.cs /nostdlib /nowarn /optmize /out 指定输出文件 如:csc /out:h.exe hee.cs /recurse csc /target:library /out:dir2.dll /recurse:dir1\dir2\*.cs /reference csc /r:system.dll;myexec.exe;myblib.dll my.cs 添加DLL /target ● /target:exe csc /target:exe my.cs ● /target:library csc /target:library my.cs ● /target:winexe csc /target:winexe my.cs ● /target:module csc /target:module my.cs resource 同linkresource功能相反,他是把.NET 资源嵌入到输出文件中 /unsafe csc/unsafe my.cs /warn csc /warn:0 my.cs 0: 关闭警告 1: 只显示严重警告 2: 级别为1的和不严重的 3: 级别为2的和不严重的 4: 级别为3的和不严重的 /warnaserror csc /warnaserror myj.cs 把警告当错误 /win32icon csc /win32icon:myicon.ico mycs 插入图标文件 /win32res 插入一个WIN32资源到输出文件中 csc /win32res:winrf.res mt.cs
2.2 值类型
C# 值类型分三种 ● 简单类型 (Simple types) ● 结构类型 (Struck types) ● 枚举类型 (Enumeration types) 2.2.1
简单类型 (Simple types) 分为布尔型和数值型,C# 的布尔型和数值型是严格区分的,布尔型只有true 和false 两种取值,不能和整数转换 数值型分为:整值(sbyte,byte,short,ushort,int,uint,long,ulong,char),浮点(float,double), decimal 三种, 2.2.2 结构类型 (Struck types)
如下代码: --------------------- struct Student{ public string names; public uint ages; public float averagescore; public struct address{ public string city; public string street; uint NO; } } Student1;
---------------------
可以如下访问: Student1.address.city="beijing";
2.2.3 枚举类型 (Enumeration types)
enum Color{ Red,Blue,Green,White,Black };
按照系统默认:第一个为0 依次加1,也可按照自己需要定值
2.3 引用类型
C# 有以下的引用类型 ● 对象类型 ● 类类型 ● 接口 ● 代表元 ● 字符串类型 ● 数组 2.3.1 对象类型
如:object etcObject=1243;
2.3.2 类类型 一个类类型包含数据成员,函数成员,嵌套类型,数据成员是常量,静态字段和事件。函数成员包括方法,属性,索引,操作浮,构造函数,析构函数。 C# 只允许单继承,但是可以派生自多个接口
2.3.3 接口
2.3.4 代表(Delegate) 如下: ------------------------ using System; delegate int TheDelegate(); //声明一个代表 public class TheClass { public int InstanceMethod() { Console.Write("Call the instance method"); return 0; } static public int StaticMethod() { Console.Write("Call the Static method"); return 0; } } public class Test{ static public void Main(){ TheClass p=new TheClass(); //将代表指向非静态的方法 InstanceMethod TheDelegate d=new TheDelegate(p.InstanceMethod);//调用非静态方法 d();//将代表指向的静态的方法StaticMethod d=new TheDelegate(TheClass.StaticMethod); //调用静态方法 d(); } } --------------------- 2.3.5 字符串类型 2.3.6 数组 string[] etcStr={"A","B","C"}; = etcStr[0]="A";etcStr[1]="B";etcStr[2]="C";
int[,] arr={{0,1},{2,3},{4,5}} = arr[0,0]=0;arr[0,1]=1; arr[1,0]=2;arr[1,1]=3; arr[2,0]=4;arr[2,2]=5;
int nVar=5; int[] arrToo=new int[nVar]; 也可以!
2.4 装箱和折箱 2.2.1 装箱 装箱就是把值类型隐式的转化为对象类型(Object) int Number=10; 在栈中 object objNumber=number; 在堆中
装箱后Number和objNumber没有然后联系!
2.4.2 折箱 将一个对象显示的转换为一个值类型! int NOTobjectNumber=(int)objNumber;
2.5 常量和变量 2.5.1 变量
和C/C++ 不同的是可以用@ 来说明一个关键字也是一个变量!C# 没有全局变量!
2.5.2 常量 可以是然后一种值类型和引用类型!
2.6 流程控制 2.6.1 条件语句 1。 IF 语句 2。 switch 语句 在C# 中,如果没有碰到break,C#不会自动从一个CASE 遍历到下一个CASE,如果没有break 或goto,编译器会报错!和C/C++不同的还有一点是 switch 支持字符串来做为表达式。C# 的switch 语句里可以是string 类型!
2.6.2 循环语句 C# 中引入了VB 里的foreach 如下: ----------------------- using System; using System.Collections;
namespace UseForeach { /// <summary> /// Class1 的摘要说明。 /// </summary> class Test { static void WriteList(ArrayList list) { foreach(object o in list) { int i=(int)o; Console.Write(o); Console.WriteLine(++i); } } static void Main() { ArrayList list=new ArrayList(); for(int i=0;i<10;i++) list.Add(i); WriteList(list); } } } -------------------------
2.6.3 条件编译 预处理方法和条件属性 1 预处理方法 -------------------------- using System; public class SquareSample { public void CaleSquare(int nSideLength,out int nSquared) { nSquared=nSideLength*nSideLength; } public int CalcSquare(int nSideLength) { return nSideLength*nSideLength; } }
class SquareApp { public static void Main() { SquareSample sq=new SquareSample(); int nSquared=0; #if CALC_W_OUT_PARAM sq.CalcSquare(20.out nSquared); #else nSquared=sq.CalcSquare(15); #endif Console.WriteLine(nSquared.ToString()); } } //由于没有定义CALC_W_OUT_PARAM,所以编译的时候要用如下的命令 //csc /define:CALC_W_OUT_PARAM define.cs ----------------------------
------------------------------
#define DEBUG #define RELEASE #define DEMOVERSION #if DEMOVERSION &&!DEBUG #warning You are Building a demo version #endif #if DEBUG && DEMOVERSION #error You cannot build a debug demoversion #endif using System; class Demo { public static void Main() { Console.Write("Demo Application");
} } --------------------------------
-------------------------------------
//#define DEBUG #define RELEASE #define DEMOVERSION #if DEBUG #undef DEMOVERSION #endif using System; class Demo { public static void Main() { #if DEBUG Console.WriteLine("Debug version"); #elif RELEASE &&!DEMOVERSINO Console.WriteLine("Full release version"); #else Console.WriteLine("DEMO VERSION"); #endif } } (我对结果不了解) ----------------------------------
上面就是几种预处理方法的代码
2.条件属性 ---------------------------------- #define DEBUG using System; class Info { [conditional("DEBUG")] public static void Trace(string strMessage) { Console.WriteLine(strMessage); } [conditional("DEBUG")] public static void TraceX(string strFormat,params object&#;list) { Console.WriteLine(stringFormat,list); } } class TestConditional { public static void Main() { Info.Trace("Cool"); Info.TraceX("{0{ {1} {2}","C","U",2001); } }
// 有错误? --------------------------------

|