+數據類型/變量/表達式
enum Range:long{Max=23232323,Min=255L};
struct屬於簡單類型,不能繼承.
--值類型包括枚舉類型和結構類型.
--引用類型包括類類型/接口類型/數組類型/代理類型/預定義的object和string類型.
----數組:
using System;
class Array
{
public static void Main()
{
//一維數組,聲明的同時初始化
int[] I={5,10,15};
Console.WriteLine("I[]={0},{1},{2}",I[0],I[1],I[2]);
//以一維數組作爲元素的一維數組,new運算符來實例化
bool[][] B=new bool[2][];
B[0]=new bool[2]{true,false};
B[1]=new bool[1]{true};
Console.WriteLine("B[0][1]={0}",B[0][1]);
//二維數組,初始化
byte[,] Y=new byte[2,2]{{0,1},{2,3}};
Console.WriteLine("Y[,0]={0},{1}",Y[0,0],Y[1,0]);
//一維字符串數組
string[] S=new string[3];
S[0]="Zhang";
S[1]="Zhu";
Console.WriteLine("{0},{1}",S[0],S[1]);
}
}
----裝箱和拆箱
裝箱:將一個值類型隱式的轉換爲object類型(或者任何由值類型實現的接口類型)
int i=10;
object obj=i;//裝箱轉換,值->引用
int j=(int)obj;//拆箱轉換,引用->值
---特殊運算符
as
is
new
sizeof
typeof
stackalloc
checked溢出檢查
unchecked
+流程控制
switch語句:
---若無goto case 標號;和goto default;語句每個case分支後面要有break;否則編譯報錯.
Console.WriteLine("Size:1=small 2=medium 3=large");
Console.Write("Please enter your selection:");
string s=Console.ReadLine();
switch(s)
{
case "0":goto case "1";//goto case 標號;
case "1":Console.WriteLine("small size.");break;
case "2":Console.WriteLine("medium size.");break;
case "3":Console.WriteLine("large size.");break;
case "4":goto default;//goto default;
default:Console.WriteLine("invalid selection.");break;
}
---break,continue,goto,return,throw
---foreach
(1) int odd=0,even=0;
int[] arr=new int[]{0,1,2,3,4,5,6,12,34,56,44,68,98,77};
foreach(int i in arr)
{
if(i%2==0)even++;
else odd++;
}
Console.WriteLine("odd={0},even={1}",odd,even);
(2)
Hashtable ziphash=new Hashtable();
ziphash.Add("100001","Beijing");
ziphash.Add("200001","Shanghai");
ziphash.Add("300001","Guangzhou");
ziphash.Add("710001","Xian");
ziphash.Add("000001","Dalian");
ziphash.Add("000002","Qingdao");
Console.WriteLine("Zip code/tCity");
Console.WriteLine("-------/t/t-------");
foreach(string zip in ziphash.Keys)
{
Console.WriteLine(zip+"/t/t"+ziphash[zip]);
}
(3)
using System;
public class MyCollection
{
int[] items;
public MyCollection()
{
items=new int[5]{12,44,33,2,50};
}
public MyEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}
public class MyEnumerator
{
int nIndex;
MyCollection collection;
public MyEnumerator(MyCollection coll)
{
collection=coll;
nIndex=-1;
}
public bool MoveNext()
{
nIndex++;
return(nIndex<collection.items.GetLength(0));
}
public int Current //屬性
{get{return(collection.items[nIndex]);}}
}
}
public class MainClass
{
public static void Main()
{
MyCollection col=new MyCollection();
Console.WriteLine("Values in col:");
foreach(int i in col){
Console.Write("{0}/t",i);
}
}
}
---預處理指令
C#編譯器沒有單獨的預處理器,
#if #elif #else #endif
#define #undef
#warning
#error
#line
#region #endregion
---附加屬性和條件
[A][B] [B][A] [A,B] [B,A]是等價的.
#define DEBUG
#undef DEBUG
using System;
using System.Diagnostics;
public class Trace
{
[Conditional("DEBUG")]public static void Msg(string msg)
{
Console.WriteLine(msg);
}
}
class Test
{
static void A()
{
Trace.Msg("Now in A()");
}
public static void Main()
{
Trace.Msg("Now in Main.");
A();
Console.WriteLine("Done");
}
}
---異常處理
C#中,所有的異常必須是一個從System.Exception派生的類類型的實例.
所有異常的共有屬性:Message和InnerException