PS:在網上看到的一篇C#總結,英文的,總結的還可以,都是基礎知識,翻譯給大家學習。文章結尾有英文原版。發布地址:http://www.cnblogs.com/zxlovenet/p/3745903.html
1.數據類型
類型名 |
大小 |
示例 |
String |
2字節/字符 |
S=”reference”; |
Bool |
|
b=true; |
char |
2字節 |
ch=’a’; |
byte |
1字節 |
b=0x78; |
short |
2字節 |
lval=54; |
int |
4字節 |
lval=540; |
long |
8字節 |
lval=5400; |
float |
4字節 |
val=54.0F; |
double |
8字節 |
val=54.0D; |
decimal |
16字節 |
val=54.0M; |
2.數組
描述
int[] numArray = {1903, 1907, 1910}; int[] numArray = new int[3]; // 3行2列 int[ , ] nums = {{1907, 1990}, {1904, 1986}, {1910, 1980}};
發布地址:http://www.cnblogs.com/zxlovenet/p/3745903.html
數組操作
Array.Sort(numArray); // 升序排列 // 排序開始在4位置,排序10個元素 Array.Sort(numArray, 4, 10); // 使用一個數組作為鍵排序兩個數組 string[] values = {“Cary”, “Gary”, “Barbara”}; string[] keys = {“Grant”, “Cooper”, “Stanwyck”}; Array.Sort(keys, values); // 清除數組中的元素(數組元素, 第一個元素, 元素長度) Array.Clear(numArray, 0, numArray.Length); // 拷貝數組元素到另一個數組 Array.Copy(src, target, numelements);
3.字符串操作
方法 |
描述 |
Compare |
String.Compare(stra,strb,case,ci) case(布爾類型)true為不區分大小寫 ci – new CultureInfo(“en-US”) 返回結果: <0 if a<b, 0 if a=b, 1 if a>b |
IndexOf |
Str.IndexOf(val,start,num) Val – 要搜索的字符串 Start – 搜索字符串開始的位置 Num – 搜索的長度 |
LastIndexOf |
從字符串的結尾開始搜索 |
Split |
Char[] delim = {‘’,’,’}; string w = “Kim, Joanna Leslie”; //創建有三個名字的字符串數組 String[] names = w.Split(delim); |
Substring |
Mystring.Substring(ndx,len) String alpha = “abcdef”; //返回”cdef” String s = alpha.Substring(2); //返回”de” S = alpha.Substring(3,2); |
ToCharArray |
被選中的字符作為字符串轉換成字符數組 String vowel = “aeiou”; //創建5個元素的數組 Char[] c = vowel.ToCharArray(); //創建’i’和’o’的數組 Char[] c= vowel.ToCharArray(2,2); |
RePlace |
Newstr = oldstr.Replace(“old”,”new”); |
發布地址:http://www.cnblogs.com/zxlovenet/p/3745903.html
4.System.Text.StringBuilder
創建
StringBuilder sb=new StringBuilder(); StringBuilder sb=new StringBuilder(mystring); StringBuilder sb=new StringBuilder(mystring,capacity);
Mystring - 對象的初始值
Capacity – 緩沖區初始大小(字節)
Using StringBuilderMembers decimal bmi = 22.2M; int wt=168; StringBuilder sb = new StringBuilder(“My weight is ”); sb = sb.Append(wt); // 可以追加數量 sb= sb.Append(“ and my bmi is ”).Append(bmi); // 我的重量是 168 ,我的bmi(身體質量指數)指數是 22.2 sb= sb.Replace(“22.2”,”22.4”); string s = sb.ToString(); // 清除並設置新值 sb.Length=0; sb.Append(“Xanadu”);
5. DateTime 和 TimeSpan
DateTime Constructor DateTime(yr, mo, day) DateTime(yr, mo, day, hr, min, sec) DateTime bday = new DateTime(1964,12,20,11,2,0); DateTime newyr = DateTime.Parse(“1/1/2005”); DateTime currdt = DateTime.Now; // also AddHours, AddMonths, AddYears DateTime tomorrow = currdt.AddDays(1); TimeSpan diff = currdt.Subtract(bday); //從12/20/64 到 6/24/05 14795 天 Console.WriteLine(“{0}”, diff.Days); // TimeSpan(hrs, min, sec) TimeSpan ts = new TimeSpan(6, 30, 10); // also FromMinutes, FromHours, FromDays TimeSpan ts = TimeSpan.FromSeconds(120); TimeSpan ts = ts2 – ts1; // +,-,>,<,==, !=
發布地址:http://www.cnblogs.com/zxlovenet/p/3745903.html
6.格式化數字和日期值
格式項語法: {index[,alignment] [:format string]}
index – 格式化作用於列表中選定的元素。
alignment – 用最小的寬度(字符)來表示值。
format string – 包含指定要顯示格式的代碼
例子:String.Format(“價格是: {0:C2}”, 49.95); //輸出:價格是: $ 49.95
a.數字格式
格式說明符 |
模式 |
值 |
描述 |
C 或 c |
{0:C2}, 1388.55 |
$ 13888.55 |
貨幣 |
D 或 d |
{0:D5}, 45 |
00045 |
必須是整數值 |
E 或 e |
{0,9:E2},1388.55 |
139+E003 |
必須是浮點數 |
F 或 f |
{0,9:F2},1388.55 |
1388.55 |
定點表示 |
N 或 n |
{0,9:N1},1388.55 |
1,388.6 |
|
P 或 p |
{0,9:P3},.7865 |
78.650% |
|
R 或 r |
{0,9:R},3.14159 |
3.14159 |
|
X 或 x |
{0,9:X4},31 |
001f |
|
示例:
CultureInfo ci = new CultureInfo("de-DE"); // 德國文化 string curdt = String.Format(ci,"{0:M}",DateTime.Now); // 29 Juni
b. 時間格式:(2005年1月19日16:05:20)的美國格式值
格式 |
顯示值 |
格式 |
顯示值 |
d |
1/19/2005 |
Y or y |
January,25 |
D |
Wednesday,January 19,2005 |
t |
4:05 PM |
f |
Wednesday,January 19,2005 4:05:20 PM |
T |
4:05:20 PM |
F |
Wednesday,January 19,2005 4:05 PM |
s |
2005-01-19T16:05:20 |
g |
1/19/2005 4:05 PM |
u |
2005-01-19 16:05:20Z |
G |
1/19/2005 4:05:20 PM |
U |
Wednesday, January 19, 2005 21:05:20PM |
M or m |
January 19 |
|
|
發布地址:http://www.cnblogs.com/zxlovenet/p/3745903.html
7. 使用 System.Text.RegularExpressions.Regex 類
string zipexp = @"\d{5}((-|\s)?\d{4})?$"; string addr="W.44th St, New York, NY 10017-0233"; Match m = Regex.Match(addr,zipexp); //靜態方法 Regex zipRegex= new Regex(zipexp); m= zipRegex.Match(addr); // 使用正則表達式對象 Console.WriteLine(m.Value); // 10017-0233
模式 |
描述 |
范例 |
+ |
匹配一個或多個 |
ab+c 匹配 abc,abbc |
* |
匹配零個或多個 |
ab*c 匹配 ac,abbc |
? |
匹配零個或一個 |
ab?c 匹配 ac,abc |
\d \D |
匹配十進制數字或非數字(\D) |
\d\d 匹配 01,55 |
\w \D |
匹配任何單字符或非字符 |
\w 等價於 [a-zA-Z0-9_] |
\s \S |
匹配空白或非空白 |
\d*\s\d+ 匹配 246 98 |
[ ] |
匹配任何設置的字符 |
[aeiou]n 匹配 in,on |
[^ ] |
匹配沒有設置的字符 |
[^aeiou] 匹配 r 或 2 |
a | b |
a或者b |
jpg|jpeg|gif 匹配 .jpg |
\n \r \t |
換行,回車,制表 |
|
8.在命令行中使用C#編譯器
C:\> csc /t:library /out:reslib.dll mysource.cs
csc /t:winexe /r:ctls1.dll /r:ctls2.dll winapp.cs
csc /keyfile:strongkey.snk secure.cs
設置 |
描述 |
/ addmodule |
從一個執行文件中導入元數據,不包含manifest。 |
/ debug |
告訴編譯器生成調試信息。 |
/ doc |
指定一個XML文檔文件,在編譯過程中創建。 |
/ keyfile |
指定用於包含文件密鑰,創建一個強命名程序集。 |
/ lib |
指定目錄中搜索,外部引用的程序集。 |
/ out |
編譯輸出文件的名稱。 |
/reference (/r) |
引用外部程序集。 |
/resource |
資源文件在輸出中嵌入。 |
/target (/t) |
/t:exe /t:library /t:module /t:winexe |
發布地址:http://www.cnblogs.com/zxlovenet/p/3745903.html
9.C#語言基礎
控制流語句 |
|
switch (表達式) { case 表達式: // 語句 break / goto / return() case ... default: // 語句 break / goto / return() } 表達式可以是 整形、字符串或枚舉類型。 |
switch (類型) { case “vhs”: price= 10.00M; break; case “dvd”: price=16.00M; break; default: price=12.00M: break; } |
if (條件) { // 語句 } else { // 語句 } |
if (genre==”vhs”) price=10.00M; else if (genre==”dvd”) price=16.00M; else price=12.00M; |
循環結構 |
|
while (條件) { body } do { body } while (條件); |
while ( ct < 8) { tot += ct; ct++; } do { tot += ct; ct++;} while (ct < 8); |
循環結構(續) |
|
for (初始值;終止條件;迭代;) { // 語句 } foreach (類型 變量名 in 集合) { // 語句 } |
for (int i=0;i<8;i++) { tot += i; } int[] ages = {27, 33, 44}; foreach(int age in ages) { tot += age; } |
10.C#類定義
類 |
[public | protected | internal | private] [abstract | sealed | static] class 類名 [:繼承類/接口] |
構造函數 |
[修飾符權限] 類名 (參數) [:initializer] initializer –在基類中調用基構造函數。 這就要求在類的構造函數。 public class Shirt: Apparel { public Shirt(decimal p, string v) : base(p,v) { constructor body } |
方法 |
[修飾符權限] [static | virtual | override | new | sealed | abstract ] 方法名 ( 參數列表) { 主體 } virtual –方法可以在子類中被覆蓋。 override –在重寫基類的虛方法。 new –在基類中隱藏了非虛擬方法。 sealed –防止派生類繼承。 abstract –必須由子類實現。 傳遞參數: a. 默認情況下,參數是按值傳遞。 b. ref和out修飾符:通過引用傳遞 string id= “gm”; // 調用者初始化ref int weight; // 被調用方法初始化 GetFactor(ref id, out weight); // ... 其它代碼 static void GetFactor(ref string id, out int wt) { if (id==”gm”) wt = 454; else wt=1; return; } Prope |
屬性 |
[修飾符] <數據類型> 屬性名稱{ public string VendorName { get { return vendorName; } set { vendorName = value; } } |
11. 委托和事件
委托
[修飾符] delegate 返回值類型 委托名稱 ([參數列表]);
// (1) 定義一個委托調用方法(S)具有單個字符串參數 public delegate void StringPrinter(string s); // (2) 注冊方法通過委托調用 StringPrinter prt = new StringPrinter(PrintLower); prt += new StringPrinter(PrintUpper); prt(“Copyright was obtained in 2005”); / /執行PrintLower和PrintUpper 使用匿名方法與委托,而不是調用一個方法,委托封裝的代碼被執行: prt = delegate(string s) { Console.WriteLine(s.ToLower()); }; prt += delegate(string s) { Console.WriteLine(s.ToUpper()); }; prt(“Print this in lower and upper case.”);
事件
// class.event += new delegate(event handler method); Button Total = new Button(); Total.Click += new EventHandler(GetTotal); //事件處理程序方法必須已經指定由委托簽名 private void GetTotal( object sender, EventArgs e) {}
常用的控件事件
事件 |
委托 |
Click, MouseEnter DoubleClick, MouseLeave |
EventHandler( object sender, EventArgs e) |
MouseDown, Mouseup, MouseMove |
MouseEventHandler(object sender,MouseEventArgs e) e.X, e.Y – x和y坐標 e.Button – MouseButton.Left, Middle, Right |
KeyUp, KeyDown |
KeyEventHandler(object sndr, KeyEventArgs e) e.Handled –表示事件是否被處理。 e.KeyCode –Keys枚舉,例如,Keys.V e.Modifiers –表示如果Alt鍵,Ctrl或Shift鍵。 |
KeyPress |
KeyPressEventHandler(object sender,KeyPressEventArgs e) |
12. 結構體
[屬性][修飾符] 結構體名稱 [:接口] { 結構體主體}
與類的區別:
1.是值類型•不能從一個類繼承或繼承
2.字段不能有初始值設定•顯式構造函數必須有一個參數
13. 枚舉 (被枚舉的類型)
枚舉 |
枚舉操作 |
e num Fabric: int { cotton = 1, silk = 2, wool = 4, rayon = 8 } |
int cotNum = (int) Fabric.cotton; // 1 string cotName = Fabric.cotton.ToString(); // cotton string s = Enum.GetName(typeof(Fabric),2); // silk //創建wool枚舉實例,如果它是有效的 if(Enum.IsDefined(typeof(Fabric), “wool”) Fabric woolFab = (Fabric)Enum.Parse(typeof(Fabric),”wool”); |