第1 章.NET 體系結(jié)構(gòu)..................................................................................................................................7
1.1 C#與.NET 的關(guān)系............................................................................................................................. 7
1.2 公共語言運(yùn)行庫................................................................................................................................7
1.2.1 平臺(tái)無關(guān)性.............................................................................................................................8
1.2.2 提高性能.................................................................................................................................8
1.2.3 語言的互操作性.....................................................................................................................8
1.3 中間語言............................................................................................................................................9
1.3.1 面向?qū)ο蠛徒涌诘闹С?......................................................................................................10
1.3.2 相異值類型和引用類型.......................................................................................................10
1.3.3 強(qiáng)數(shù)據(jù)類型...........................................................................................................................11
1.3.4 通過異常處理錯(cuò)誤...............................................................................................................15
1.3.5 特性的使用...........................................................................................................................15
1.4 程序集..............................................................................................................................................16
1.4.1 私有程序集...........................................................................................................................16
1.4.2 共享程序集...........................................................................................................................16
1.4.3 反射.......................................................................................................................................17
1.5 .NET Framework 類.........................................................................................................................17
1.6 命名空間..........................................................................................................................................18
1.7 用C#創(chuàng)建.NET 應(yīng)用程序.............................................................................................................. 18
1.7.1 創(chuàng)建ASP.NET 應(yīng)用程序.....................................................................................................18
1.7.2 創(chuàng)建Windows 窗體............................................................................................................. 19
1.7.3 使用Windows Presentation Foundation(WPF)....................................................................20
1.7.4 Windows 控件...................................................................................................................... 20
1.7.5 Windows 服務(wù)...................................................................................................................... 20
1.7.6 Windows Communication Foundation(WCF)...................................................................... 20
1.8 C#在.NET 企業(yè)體系結(jié)構(gòu)中的作用............................................................................................... 20
1.9 小結(jié)..................................................................................................................................................21
第2 章C# 基礎(chǔ)........................................................................................................................................... 23
2.1 引言..................................................................................................................................................23
2.2 第一個(gè)C#程序................................................................................................................................ 23
2.2.1 代碼.......................................................................................................................................23
2.2.2 編譯并運(yùn)行程序...................................................................................................................24
2.2.3 詳細(xì)介紹...............................................................................................................................24
2.3 變量..................................................................................................................................................26
2.3.1 變量的初始化.......................................................................................................................26
2.3.2 類型推斷...............................................................................................................................27
2.3.3 變量的作用域.......................................................................................................................28
2.3.4 常量.......................................................................................................................................30
2.4 預(yù)定義數(shù)據(jù)類型..............................................................................................................................31
2.4.1 值類型和引用類型...............................................................................................................31
2.4.2 CTS 類型.............................................................................................................................. 32
2.4.3 預(yù)定義的值類型...................................................................................................................32
2.4.4 預(yù)定義的引用類型...............................................................................................................34
2.5 流控制..............................................................................................................................................36
2.5.1 條件語句...............................................................................................................................36
2.5.2 循環(huán).......................................................................................................................................39
2.5.3 跳轉(zhuǎn)語句...............................................................................................................................42
2.6 枚舉..................................................................................................................................................43
2.7 數(shù)組..................................................................................................................................................45
2.8 命名空間..........................................................................................................................................45
2.8.1 using 語句............................................................................................................................. 46
2.8.2 命名空間的別名...................................................................................................................47
2.9 Main()方法.......................................................................................................................................48
2.9.1 多個(gè)Main()方法...................................................................................................................48
2.9.2 給Main()方法傳送參數(shù).......................................................................................................49
2.10 有關(guān)編譯C#文件的更多內(nèi)容...................................................................................................... 49
2.11 控制臺(tái)I/O..................................................................................................................................... 51
2.12 使用注釋........................................................................................................................................52
2.12.1 源文件中的內(nèi)部注釋.........................................................................................................52
2.12.2 XML 文檔說明...................................................................................................................53
2.13 C#預(yù)處理器指令........................................................................................................................... 54
2.13.1 #define 和#undef...............................................................................................................55
2.13.2 #if, #elif, #else 和#endif......................................................................................................55
2.13.3 #warning 和# error............................................................................................................ 56
2.13.4 #region 和#endregion..........................................................................................................56
2.13.5 #line.....................................................................................................................................56
2.13.6 #pragma...............................................................................................................................57
2.14 C#編程規(guī)則................................................................................................................................... 57
2.14.1 用于標(biāo)識(shí)符的規(guī)則.............................................................................................................57
2.14.2 用法約定.............................................................................................................................58
2.15 小結(jié)................................................................................................................................................62
第3 章對(duì)象和類型......................................................................................................................................63
3.1 類和結(jié)構(gòu)..........................................................................................................................................63
3.2 類成員..............................................................................................................................................64
3.2.1 數(shù)據(jù)成員...............................................................................................................................64
3.2.2 函數(shù)成員...............................................................................................................................65
3.2.3 只讀字段...............................................................................................................................76
3.3 匿名類型..........................................................................................................................................77
3.4 結(jié)構(gòu)..................................................................................................................................................77
3.4.1 結(jié)構(gòu)是值類型.......................................................................................................................79
3.4.2 結(jié)構(gòu)和繼承...........................................................................................................................79
3.4.3 結(jié)構(gòu)的構(gòu)造函數(shù)...................................................................................................................79
3.5 部分類..............................................................................................................................................80
3.6 靜態(tài)類..............................................................................................................................................80
3.7 Object 類.......................................................................................................................................... 81
3.7.1 System.Object 方法.............................................................................................................. 81
3.7.2 ToString()方法......................................................................................................................82
3.8 擴(kuò)展方法..........................................................................................................................................83
3.9 小結(jié)..................................................................................................................................................84
第4 章繼承.............................................................................................................................................84
4.1 繼承的類型......................................................................................................................................84
4.1.1 實(shí)現(xiàn)繼承和接口繼承...........................................................................................................84
4.1.2 多重繼承...............................................................................................................................85
4.1.3 結(jié)構(gòu)和類...............................................................................................................................85
4.2 實(shí)現(xiàn)繼承..........................................................................................................................................85
4.2.1 虛方法...................................................................................................................................86
4.2.2 隱藏方法...............................................................................................................................87
4.2.3 調(diào)用函數(shù)的基類版本...........................................................................................................88
4.2.4 抽象類和抽象函數(shù)...............................................................................................................88
4.2.5 密封類和密封方法...............................................................................................................89
4.2.6 派生類的構(gòu)造函數(shù)...............................................................................................................89
4.3 修飾符..............................................................................................................................................93
4.3.1 可見性修飾符.......................................................................................................................93
4.3.2 其他修飾符...........................................................................................................................94
4.4 接口..................................................................................................................................................95
4.4.1 定義和實(shí)現(xiàn)接口...................................................................................................................96
4.4.2 派生的接口...........................................................................................................................98
4.5 小結(jié)................................................................................................................................................100
第5 章數(shù)組...........................................................................................................................................101
5.1 簡(jiǎn)單數(shù)組........................................................................................................................................101
5.1.1 數(shù)組的聲明.........................................................................................................................101
5.1.2 數(shù)組的初始化.....................................................................................................................101
5.1.3 訪問數(shù)組元素.....................................................................................................................102
5.1.4 使用引用類型.....................................................................................................................102
5.2 多維數(shù)組........................................................................................................................................103
5.3 鋸齒數(shù)組........................................................................................................................................104
5.4 Array 類......................................................................................................................................... 105
5.4.1 屬性.....................................................................................................................................105
5.4.2 創(chuàng)建數(shù)組.............................................................................................................................105
5.4.3 復(fù)制數(shù)組.............................................................................................................................106
5.4.4 排序.....................................................................................................................................107
5.5 數(shù)組和集合接口............................................................................................................................110
5.5.1 IEumerable 接口................................................................................................................. 110
5.5.2 ICollection 接口..................................................................................................................110
5.5.3 IList 接口............................................................................................................................ 110
5.6 枚舉................................................................................................................................................110
5.6.1 IEnumerator 接口................................................................................................................111
5.6.2 foreach 語句........................................................................................................................111
5.6.3 yield 語句............................................................................................................................111
5.7 小結(jié)................................................................................................................................................116
第6 章運(yùn)算符和類型強(qiáng)制轉(zhuǎn)換................................................................................................................116
6.1 運(yùn)算符............................................................................................................................................116
6.1.1 運(yùn)算符的簡(jiǎn)化操作.............................................................................................................117
6.1.2 條件運(yùn)算符.........................................................................................................................118
6.1.3 checked 和unchecked 運(yùn)算符............................................................................................119
6.1.4 is 運(yùn)算符............................................................................................................................. 119
6.1.5 as 運(yùn)算符............................................................................................................................ 120
6.1.6 sizeof 運(yùn)算符...................................................................................................................... 120
6.1.7 typeof 運(yùn)算符......................................................................................................................120
6.1.8 可空類型和運(yùn)算符.............................................................................................................120
6.1.9 空接合運(yùn)算符.....................................................................................................................121
6.1.10 運(yùn)算符的優(yōu)先級(jí)...............................................................................................................121
6.2 類型的安全性................................................................................................................................121
6.2.1 類型轉(zhuǎn)換.............................................................................................................................122
6.2.2 裝箱和拆箱.........................................................................................................................124
6.3 對(duì)象的相等比較............................................................................................................................125
6.3.1 引用類型的相等比較.........................................................................................................125
6.3.2 值類型的相等比較.............................................................................................................126
6.4 運(yùn)算符重載....................................................................................................................................126
6.4.1 運(yùn)算符的工作方式.............................................................................................................127
6.4.2 運(yùn)算符重載的示例:Vector 結(jié)構(gòu)..................................................................................... 128
6.5 用戶定義的數(shù)據(jù)類型轉(zhuǎn)換............................................................................................................134
6.5.1 執(zhí)行用戶定義的類型轉(zhuǎn)換.................................................................................................135
6.5.2 多重?cái)?shù)據(jù)類型轉(zhuǎn)換.............................................................................................................139
6.6 小結(jié)................................................................................................................................................142
第7 章委托和事件....................................................................................................................................143
7.1 委托................................................................................................................................................143
7.1.1 在C#中聲明委托............................................................................................................... 144
7.1.2 在C#中使用委托............................................................................................................... 145
7.1.3 簡(jiǎn)單的委托示例.................................................................................................................147
7.1.4 BubbleSorter 示例.............................................................................................................. 149
7.1.5 多播委托.............................................................................................................................151
7.1.6 匿名方法.............................................................................................................................154
7.1.7 (表達(dá)式.............................................................................................................................. 155
7.1.8 協(xié)變和抗變.........................................................................................................................157
7.2 事件................................................................................................................................................158
7.2.1 從接收器的角度討論事件.................................................................................................159
7.2.2 生成事件.............................................................................................................................160
7.3 小結(jié)................................................................................................................................................164
第8 章字符串和正則表達(dá)式....................................................................................................................165
8.1 System.String 類............................................................................................................................ 165
8.1.1 創(chuàng)建字符串.........................................................................................................................166
8.1.2 StringBuilder 成員.............................................................................................................. 168
8.1.3 格式化字符串.....................................................................................................................169
8.2 正則表達(dá)式....................................................................................................................................174
8.2.1 正則表達(dá)式概述.................................................................................................................174
8.2.2 RegularExpressionsPlayaround 示例..................................................................................175
8.2.3 顯示結(jié)果.............................................................................................................................177
8.2.4 匹配、組合和捕獲.............................................................................................................178
8.3 小結(jié)................................................................................................................................................179
第9 章泛型...........................................................................................................................................180
9.1 概述................................................................................................................................................181
9.1.1 性能.....................................................................................................................................181
9.1.2 類型安全.............................................................................................................................182
9.1.3 二進(jìn)制代碼的重用.............................................................................................................182
9.1.4 代碼的擴(kuò)展.........................................................................................................................183
9.1.5 命名約定.............................................................................................................................183
9.2 創(chuàng)建泛型類....................................................................................................................................183
9.3 泛型類的特性................................................................................................................................187
9.3.1 默認(rèn)值.................................................................................................................................188
9.3.2 約束.....................................................................................................................................188
9.3.3 繼承.....................................................................................................................................190
9.3.4 靜態(tài)成員.............................................................................................................................191
9.4 泛型接口........................................................................................................................................191
9.5 泛型方法........................................................................................................................................192
9.6 泛型委托........................................................................................................................................194
9.6.1 執(zhí)行委托調(diào)用的方法.........................................................................................................194
9.6.2 對(duì)Array 類使用泛型委托..................................................................................................196
9.7 Framework 的其他泛型類型........................................................................................................ 198
9.7.1 結(jié)構(gòu)Nullable............................................................................................................... 198
9.7.2 EventHandler...............................................................................................199
9.8 小結(jié)................................................................................................................................................200
第10 章集合..............................................................................................................................................200
10.1 集合接口和類型..........................................................................................................................201
10.2 列表..............................................................................................................................................203
10.2.1 創(chuàng)建列表...........................................................................................................................205
10.2.2 只讀集合...........................................................................................................................212
10.3 隊(duì)列..............................................................................................................................................212
10.4 棧..................................................................................................................................................216
10.5 鏈表..............................................................................................................................................217
10.6 有序表..........................................................................................................................................222
10.7 字典..............................................................................................................................................224
10.7.1 鍵的類型...........................................................................................................................224
10.7.2 字典示例...........................................................................................................................225
10.7.3 Lookup 類......................................................................................................................... 228
10.7.4 其他字典類.......................................................................................................................229
10.8 HashSet........................................................................................................................................ 230
10.9 位數(shù)組..........................................................................................................................................233
10.9.1 BitArray.............................................................................................................................233
10.9.2 BitVector32....................................................................................................................... 235
10.10 性能............................................................................................................................................237
10.11 小結(jié)............................................................................................................................................238
第11 章Language Integrated Query..........................................................................................................239
11.1 LINQ 概述........................................................................................................................... 240
11.1.1 使用List的查詢.........................................................................................................240
11.1.2 擴(kuò)展方法...........................................................................................................................251
11.1.3 λ表達(dá)式.............................................................................................................................253
11.1.4 LINQ 查詢........................................................................................................................ 254
11.1.5 推遲查詢的執(zhí)行...............................................................................................................255
11.2 標(biāo)準(zhǔn)的查詢操作符..............................................................................................................258
11.2.1 過濾...................................................................................................................................259
11.2.2 用索引來過濾...................................................................................................................260
11.2.3 類型過濾...........................................................................................................................260
11.2.4 復(fù)合的from 子句............................................................................................................. 261
11.2.5 排序...................................................................................................................................263
11.2.6 分組...................................................................................................................................264
11.2.7 對(duì)嵌套的對(duì)象分組...........................................................................................................266
11.2.8 連接...................................................................................................................................267
11.2.9 設(shè)置操作...........................................................................................................................270
11.2.10 分區(qū).................................................................................................................................271
11.2.11 合計(jì)操作符.....................................................................................................................273
11. 2 . 12 轉(zhuǎn)換.................................................................................................................... 275
11.2.13 生成操作符.....................................................................................................................277
11.3 表達(dá)式樹..............................................................................................................................278
11.4 LINQ 提供程序................................................................................................................... 282
11.5 小結(jié)......................................................................................................................................283 |
|