# 演練:Office 編程(C# 和 Visual Basic)
Visual Studio 2010 在 C# 和 Visual Basic 中引入了改進 Microsoft Office 編程的新功能。每種語言都增加了其他語言中已經存在的功能。
C# 中的新功能包括命名參數和可選參數、具有 **dynamic** 類型的返回值,以及在 COM 編程中忽略 **ref** 關鍵字和訪問索引屬性的功能。Visual Basic 中的新功能包括自動實現的屬性、Lambda 表達式語句和集合初始值設定項。
兩種語言都支持嵌入類型信息,從而允許在不向用戶的計算機部署主互操作程序集 (PIA) 的情況下部署與 COM 組件交互的程序集。有關詳細信息,請參閱[演練:嵌入托管程序集中的類型(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd409610.aspx)。
本演練演示 Office 編程環境中的新功能,但這些新功能在常規編程中也極為有用。在本演練中,你將首先使用 Excel 外接應用程序來創建 Excel 工作簿。然后,你將創建包含工作簿鏈接的 Word 文檔。最后,你將看到可以如何開啟和關閉 PIA 依賴項。
## 系統必備
若要完成本演練,你的計算機上必須安裝有 Microsoft Office Excel 2013(或者 2007 或更高版本)和 Microsoft Office Word 2013(或者 2007 或更高版本)。
如果你使用的操作系統早于 Windows Vista,請確保安裝 .NET Framework 2.0。
|  注意 |
| :-- |
| 以下說明中的某些 Visual Studio 用戶界面元素在計算機上出現的名稱或位置可能會不同。 這些元素取決于你所使用的 Visual Studio 版本和你所使用的設置。 有關詳細信息,請參閱[個性化 Visual Studio IDE](https://msdn.microsoft.com/zh-CN/library/mt269425.aspx)。 |
## 設置 Excel 外接應用程序
1. 啟動 Visual Studio。
2. 在**“文件”**菜單上,指向**“新建”**,然后單擊**“項目”**。
3. 在“安裝的模板”窗格中,展開“Visual Basic”或“Visual C#”,再展開“Office”,然后單擊“2013”(或“2010”或“2007”)。
4. 在“模板”窗格中,單擊“Excel 2013 外接應用程序”(或“Excel 2010 外接應用程序”或“Excel 2007 外接應用程序”)。
5. 查看“模板”窗格的頂部,確保“.NET Framework 4”或更高版本出現在“目標框架”框中。
6. 如果需要,在“名稱”框中鍵入項目的名稱。
7. 單擊“確定”。
8. 新項目將出現在“解決方案資源管理器”中。
## 添加引用
1. 在“解決方案資源管理器”中,右鍵單擊你的項目名稱,然后單擊“添加引用”。將顯示**“添加引用”**對話框。
2. 在“程序集”選項卡上,在“組件名稱”列表中選擇“Microsoft.Office.Interop.Excel”版本 15.0.0.0(針對 Excel 2010,選擇版本 14.0.0.0;針對 Excel 2007,選擇版本 12.0.0.0),然后按住 Ctrl 鍵并選擇“Microsoft.Office.Interop.Word”版本 15.0.0.0(針對 Word 2010,選擇版本 14.0.0.0;針對 Word 2007,選擇版本 12.0.0.0)。如果未看到程序集,你可能需要確保安裝并顯示它們(參閱[如何:安裝 Office 主互操作程序集](https://msdn.microsoft.com/zh-CN/library/kh3965hw.aspx))。
3. 單擊“確定”。
## 添加必要的 Imports 語句或 using 指令
1. 在“解決方案資源管理器”中,右鍵單擊“ThisAddIn.vb”或“ThisAddIn.cs”文件,然后單擊“查看代碼”。
2. 將以下 **Imports** 語句 (Visual Basic) 或 **using** 指令 (C#) 添加到代碼文件的頂部(如果不存在)。
[VB](https://msdn.microsoft.com/zh-CN/library/ee342218.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1)
```
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
```
## 創建銀行帳戶列表
1. 在“解決方案資源管理器”中,右鍵單擊你的項目名稱,單擊“添加”,然后單擊“類”。如果使用的是 Visual Basic,則將類命名為 Account.vb;如果使用的是 C#,則將類命名為 Account.cs。單擊**“添加”**。
2. 將 Account 類的定義替換為以下代碼。類定義使用_“自動實現的屬性”_,在 Visual Studio 2010 中,是 Visual Basic 的新功能。有關詳細信息,請參閱[自動實現的屬性 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd293589.aspx)。
[VB](https://msdn.microsoft.com/zh-CN/library/ee342218.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2)
```
class Account
{
public int ID { get; set; }
public double Balance { get; set; }
}
```
3. 若要創建包含兩個帳戶的 bankAccounts 列表,請將以下代碼添加到 ThisAddIn.vb 或 ThisAddIn.cs 中的 ThisAddIn_Startup 方法。列表聲明使用_“集合初始值設定項”_,在 Visual Studio 2010 中,是 Visual Basic 的新功能。有關詳細信息,請參閱[集合初始值設定項 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd293617.aspx)。
[VB](https://msdn.microsoft.com/zh-CN/library/ee342218.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-3)
```
var bankAccounts = new List<Account>
{
new Account
{
ID = 345,
Balance = 541.27
},
new Account
{
ID = 123,
Balance = -127.44
}
};
```
## 將數據導出到 Excel
1. 在相同的文件中,將以下方法添加到 ThisAddIn 類。該方法設置 Excel 工作薄并將數據導出到工作簿。
[VB](https://msdn.microsoft.com/zh-CN/library/ee342218.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-4)
```
void DisplayInExcel(IEnumerable<Account> accounts,
Action<Account, Excel.Range> DisplayFunc)
{
var excelApp = this.Application;
// Add a new Excel workbook.
excelApp.Workbooks.Add();
excelApp.Visible = true;
excelApp.Range["A1"].Value = "ID";
excelApp.Range["B1"].Value = "Balance";
excelApp.Range["A2"].Select();
foreach (var ac in accounts)
{
DisplayFunc(ac, excelApp.ActiveCell);
excelApp.ActiveCell.Offset[1, 0].Select();
}
// Copy the results to the Clipboard.
excelApp.Range["A1:B3"].Copy();
}
```
此方法使用 C# 的兩項新功能。Visual Basic 中已存在這兩項功能。
* 方法 [Add](http://go.microsoft.com/fwlink/?LinkId=210910) 有一個_“可選參數”_,用于指定特定的模板。如果希望使用形參的默認值,你可以借助可選形參(Visual C# 2010 中新增)忽略該形參的實參。由于上一個示例中未發送任何參數,**Add** 將使用默認模板并創建新的工作簿。C# 早期版本中的等效語句要求占位符參數:excelApp.Workbooks.Add(Type.Missing).
有關詳細信息,請參閱[命名實參和可選實參(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/dd264739.aspx)。
* [Range](http://go.microsoft.com/fwlink/?LinkId=210911) 對象的 **Range** 和 **Offset** 屬性使用_“索引屬性”_功能。此功能允許你通過以下典型 C# 語法從 COM 類型使用這些屬性。索引屬性還允許你使用 **Range** 對象的 **Value** 屬性,因此不必使用 **Value2** 屬性。 **Value** 屬性已編入索引,但索引是可選的。在以下示例中,可選參數和索引屬性配合使用。
```
// Visual C# 2010 provides indexed properties for COM programming.
excelApp.Range["A1"].Value = "ID";
excelApp.ActiveCell.Offset[1, 0].Select();
```
在早期版本的語言中,需要以下特殊語法。
```
// In Visual C# 2008, you cannot access the Range, Offset, and Value
// properties directly.
excelApp.get_Range("A1").Value2 = "ID";
excelApp.ActiveCell.get_Offset(1, 0).Select();
```
你不能創建自己的索引屬性。該功能僅支持使用現有索引屬性。
有關詳細信息,請參閱[如何:在 COM 互操作編程中使用索引屬性(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/ee310208.aspx)。
2. 在 DisplayInExcel 的末尾添加以下代碼以將列寬調整為適合內容。
[VB](https://msdn.microsoft.com/zh-CN/library/ee342218.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-7)
```
excelApp.Columns[1].AutoFit();
excelApp.Columns[2].AutoFit();
```
這些添加展示 C# 2010 中的另一項新功能:處理從 COM 主機返回的 **Object** 值(如 Office),就像它們具有 [dynamic](https://msdn.microsoft.com/zh-CN/library/dd264741.aspx) 類型一樣。當“嵌入互操作類型”設置為其默認值 **True** 時,或者由 [/link](https://msdn.microsoft.com/zh-CN/library/dd264728.aspx) 編譯器選項引用程序集時,自動發生這種情況。鍵入 **dynamic** 允許后期綁定(Visual Basic 已提供該功能)并可避免 Visual C# 2008 和早期版本的語言中要求的顯式強制轉換。
例如,excelApp.Columns[1] 返回 **Object**,并且 **AutoFit** 是 Excel 的 [Range](http://go.microsoft.com/fwlink/?LinkId=210911) 方法。如果沒有 **dynamic**,你必須將 excelApp.Columns[1] 返回的對象強制轉換為 Range 的實例,然后才能調用 AutoFit 方法。
```
// Casting is required in Visual C# 2008.
((Excel.Range)excelApp.Columns[1]).AutoFit();
// Casting is not required in Visual C# 2010.
excelApp.Columns[1].AutoFit();
```
有關嵌入互操作類型的詳細信息,請參閱本主題后面部分的“查找 PIA 引用”和“還原 PIA 依賴項”程序。有關 **dynamic** 的詳細信息,請參閱 [dynamic(C# 參考)](https://msdn.microsoft.com/zh-CN/library/dd264741.aspx) 或 [使用類型 dynamic(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/dd264736.aspx)。
## 調用 DisplayInExcel
1. 在 ThisAddIn_StartUp 方法的末尾添加以下代碼。對 DisplayInExcel 的調用包含兩個參數。第一個參數是要處理的帳戶列表的名稱。第二個參數是定義如何處理數據的多行 lambda 表達式。每個帳戶的 ID 和 balance 值都顯示在相鄰的單元格中,如果余額小于零,則相應的行顯示為紅色。多行 lambda 表達式是 Visual Basic 2010 中的新功能。有關詳細信息,請參閱[Lambda 表達式 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/bb531253.aspx)。
[VB](https://msdn.microsoft.com/zh-CN/library/ee342218.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-9)
```
DisplayInExcel(bankAccounts, (account, cell) =>
// This multiline lambda expression sets custom processing rules
// for the bankAccounts.
{
cell.Value = account.ID;
cell.Offset[0, 1].Value = account.Balance;
if (account.Balance < 0)
{
cell.Interior.Color = 255;
cell.Offset[0, 1].Interior.Color = 255;
}
});
```
2. 若要運行程序,請按 F5。出現包含帳戶數據的 Excel 工作表。
## 添加 Word 文檔
1. 在 ThisAddIn_StartUp 方法末尾添加以下代碼,以創建包含指向 Excel 工作簿的鏈接的 Word 文檔。
[VB](https://msdn.microsoft.com/zh-CN/library/ee342218.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-10)
```
var wordApp = new Word.Application();
wordApp.Visible = true;
wordApp.Documents.Add();
wordApp.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);
```
此代碼展示 C# 中的幾項新功能:在 COM 編程中忽略 **ref** 的功能、命名參數以及可選參數。Visual Basic 中已存在這些功能。 [PasteSpecial](http://go.microsoft.com/fwlink/?LinkId=147099) 方法有七個參數,每個參數都是可選引用參數。在 Visual C# 2010 之前,你必須為這七個形參定義用作實參的對象變量,即使你沒有有意義的值發送。通過命名實參和可選實參,你可以指定希望按名稱訪問的形參并僅將實參發送到這些形參。在本示例中,發送實參以指示應創建指向剪貼板上工作簿的鏈接(形參 Link)并指示該鏈接應在 Word 文檔中顯示為圖標(形參 DisplayAsIcon)。Visual C# 2010 還允許你忽略這些參數的 **ref** 關鍵字。將 Visual C# 2008 的以下代碼段與 Visual C# 2010 中需要的單行進行比較:
```
// Call to PasteSpecial in Visual C# 2008.
object iconIndex = Type.Missing;
object link = true;
object placement = Type.Missing;
object displayAsIcon = true;
object dataType = Type.Missing;
object iconFileName = Type.Missing;
object iconLabel = Type.Missing;
wordApp.Selection.PasteSpecial(ref iconIndex,
ref link,
ref placement,
ref displayAsIcon,
ref dataType,
ref iconFileName,
ref iconLabel);
// Call to PasteSpecial in Visual C# 2010.
wordApp.Selection.PasteSpecial(Link: true, DisplayAsIcon: true);
```
## 運行應用程序
1. 按 F5 運行該應用程序。Excel 啟動并顯示包含 bankAccounts 中兩個帳戶的信息的表。然后,出現包含指向 Excel 表的 Word 文檔。
## 清理已完成的項目
1. 在 Visual Studio 中,單擊“生成”菜單上的“清理解決方案”。否則,每次在計算機上打開 Excel 時都會運行外接應用程序。
## 查找 PIA 引用
1. 再次運行應用程序,但不單擊“清理解決方案”。
2. 在“開始”菜單上,單擊“所有程序”。接下來依次單擊“Microsoft Visual Studio 2013”、“Visual Studio 工具”、“Visual Studio 命令提示符 (2013)”。
3. 在 Visual Studio 命令提示符 (2013) 窗口中鍵入 **ildasm**,然后按 Enter。此時將出現 IL DASM 窗口。
4. 在 IL DASM 窗口的“文件”菜單上,單擊“打開”。雙擊“Visual Studio 2013”,然后雙擊“項目”。打開項目的文件夾,在 bin/Debug 文件夾中查找 _項目名稱_.dll。雙擊 _項目名稱_.dll。新窗口將顯示項目的屬性以及對其他模塊和程序集的引用。注意,命名空間 **Microsoft.Office.Interop.Excel** 和 **Microsoft.Office.Interop.Word** 包含在程序集中。在 Visual Studio 2013 中,編譯器默認將你需要的類型從引用的 PIA 導入程序集。
有關詳細信息,請參閱[如何:查看程序集內容](https://msdn.microsoft.com/zh-CN/library/ceats605.aspx)。
5. 雙擊“清單”圖標。此時將出現包含程序集列表的窗口,這些程序集包含項目所引用的項。 **Microsoft.Office.Interop.Excel** 和 **Microsoft.Office.Interop.Word** 未包含在列表中。由于項目需要的類型已導入程序集中,因此不需要引用 PIA。這使得部署變得更加容易。用戶的計算機上不必存在 PIA,因為應用程序不需要部署特定版本的 PIA,應用程序可設計為與多個版本的 Office 配合使用,前提是所有版本中都存在必要的 API。
由于不再需要部署 PIA,你可以提前創建可與多個版本的 Office(包括之前的版本)配合使用的應用程序。但是,僅當你的代碼不使用你當前所使用 Office 版本中不可用的任何 API 時,此情況才適用。特殊 API 在早期版本中是否可用并不始終明確,因此不建議使用早期版本的 Office。
6. 關閉清單窗口和程序集窗口。
|  注意 |
| :-- |
| 在 Office 2003 以前,Office 并不發布 PIA。因此,生成適用于 Office 2002 或早期版本的互操作程序集的唯一方法是導入 COM 引用。 |
## 還原 PIA 依賴項
1. 在“解決方案資源管理器”中,單擊“顯示所有文件”按鈕。展開“引用”文件夾并選擇“Microsoft.Office.Interop.Excel”。按 F4 以顯示**“屬性”**窗口。
2. 在“屬性”窗口中,將“嵌入互操作類型”屬性從“True”更改為“False”。
3. 對 **Microsoft.Office.Interop.Word** 重復此程序中的步驟 1 和 2。
4. 在 C# 中,在 DisplayInExcel 方法的末尾注釋掉對 Autofit 的兩次調用。
5. 按 F5 以驗證項目是否仍正確運行。
6. 重復上一個程序的步驟 1-3 以打開程序集窗口。注意,**Microsoft.Office.Interop.Word** 和 **Microsoft.Office.Interop.Excel** 不再位于嵌入程序集列表中。
7. 雙擊“清單”圖標并滾動引用程序集的列表。 **Microsoft.Office.Interop.Word** 和 **Microsoft.Office.Interop.Excel** 均位于列表中。由于應用程序引用 Excel 和 Word PIA 并且“嵌入互操作類型”屬性設置為“False”,因此最終用戶的計算機上必須存在兩個程序集。
8. 在 Visual Studio 中,單擊“生成”菜單上的“清理解決方案”以清理完成的項目。
## 請參閱
[自動實現的屬性 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd293589.aspx)
[自動實現的屬性(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/bb384054.aspx)
[集合初始值設定項 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd293617.aspx)
[對象和集合初始值設定項(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/bb384062.aspx)
[可選參數 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/f25e2b6b.aspx)
[按位置和名稱傳遞參數 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/51wfzyw0.aspx)
[命名實參和可選實參(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/dd264739.aspx)
[早期綁定和后期綁定 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/0tcf61s1.aspx)
[dynamic(C# 參考)](https://msdn.microsoft.com/zh-CN/library/dd264741.aspx)
[使用類型 dynamic(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/dd264736.aspx)
[Lambda 表達式 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/bb531253.aspx)
[Lambda 表達式(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/bb397687.aspx)
[如何:在 COM 互操作編程中使用索引屬性(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/ee310208.aspx)
[演練:嵌入 Microsoft Office 程序集中的類型信息(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/ee317478.aspx)
[演練:嵌入托管程序集中的類型(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd409610.aspx)
[演練:創建你的第一個 Excel VSTO 外接程序](https://msdn.microsoft.com/zh-CN/library/cc668205.aspx)
[COM 互操作 (Visual Basic)](https://msdn.microsoft.com/zh-CN/library/6bw51z5z.aspx)
[互操作性(C# 編程指南)](https://msdn.microsoft.com/zh-CN/library/ms173184.aspx)
- C# 編程指南
- 在 C# 程序內部
- Hello World -- 您的第一個程序(C# 編程指南)
- C# 程序的通用結構(C# 編程指南)
- C# 編碼約定(C# 編程指南)
- 數組(C# 編程指南)
- 作為對象的數組(C# 編程指南)
- 一維數組(C# 編程指南)
- 多維數組(C# 編程指南)
- 交錯數組(C# 編程指南)
- 對數組使用 foreach(C# 編程指南)
- 將數組作為參數傳遞(C# 編程指南)
- 使用 ref 和 out 傳遞數組(C# 編程指南)
- 隱式類型的數組(C# 編程指南)
- 類和結構(C# 編程指南)
- 類(C# 編程指南)
- 對象(C# 編程指南)
- 結構(C# 編程指南)
- 繼承(C# 編程指南)
- 多態性(C# 編程指南)
- 抽象類、密封類及類成員(C# 編程指南)
- 靜態類和靜態類成員(C# 編程指南)
- 成員(C# 編程指南)
- 訪問修飾符(C# 編程指南)
- 字段(C# 編程指南)
- 常量(C# 編程指南)
- 屬性(C# 編程指南)
- 方法(C# 編程指南)
- 構造函數(C# 編程指南)
- 析構函數(C# 編程指南)
- 對象和集合初始值設定項(C# 編程指南)
- 如何:使用 foreach 訪問集合類(C# 編程指南)
- 嵌套類型(C# 編程指南)
- 分部類和方法(C# 編程指南)
- 匿名類型(C# 編程指南)
- 委托(C# 編程指南)
- 使用委托(C# 編程指南)
- 帶有命名方法的委托與帶有匿名方法的委托(C# 編程指南)
- 如何:合并委托(多路廣播委托)(C# 編程指南)
- 如何:聲明、實例化和使用委托(C# 編程指南)
- 枚舉類型(C# 編程指南)
- 事件(C# 編程指南)
- 如何:訂閱和取消訂閱事件(C# 編程指南)
- 如何:發布符合 .NET Framework 準則的事件(C# 編程指南)
- 如何:在派生類中引發基類事件(C# 編程指南)
- 如何:實現接口事件(C# 編程指南)
- 如何:使用字典存儲事件實例(C# 編程指南)
- 如何:實現自定義事件訪問器(C# 編程指南)
- 異常和異常處理(C# 編程指南)
- 使用異常(C# 編程指南)
- 異常處理(C# 編程指南)
- 創建和引發異常(C# 編程指南)
- 編譯器生成的異常(C# 編程指南)
- 如何:使用 try/catch 處理異常(C# 編程指南)
- 如何:使用 finally 執行清理代碼(C# 編程指南)
- 如何:捕捉非 CLS 異常
- 文件系統和注冊表(C# 編程指南)
- 如何:循環訪問目錄樹(C# 編程指南)
- 如何:獲取有關文件、文件夾和驅動器的信息(C# 編程指南)
- 如何:創建文件或文件夾(C# 編程指南)
- 如何:復制、刪除和移動文件和文件夾(C# 編程指南)
- 如何:提供文件操作進度對話框(C# 編程指南)
- 如何:寫入文本文件(C# 編程指南)
- 如何:讀取文本文件中的內容(C# 編程指南)
- 如何:一次一行地讀取文本文件 (Visual C#)
- 如何:在注冊表中創建注冊表項 (Visual C#)
- 泛型(C# 編程指南)
- 泛型介紹(C# 編程指南)
- 泛型的優點(C# 編程指南)
- 泛型類型參數(C# 編程指南)
- 類型參數的約束(C# 編程指南)
- 泛型類(C# 編程指南)
- 泛型接口(C# 編程指南)
- 泛型方法(C# 編程指南)
- 泛型和數組(C# 編程指南)
- 泛型委托(C# 編程指南)
- 泛型代碼中的默認關鍵字(C# 編程指南)
- C++ 模板和 C# 泛型之間的區別(C# 編程指南)
- 運行時中的泛型(C# 編程指南)
- .NET Framework 類庫中的泛型(C# 編程指南)
- 泛型和反射(C# 編程指南)
- 泛型和特性(C# 編程指南)
- 索引器(C# 編程指南)
- 使用索引器(C# 編程指南)
- 接口中的索引器(C# 編程指南)
- 屬性和索引器之間的比較(C# 編程指南)
- 接口(C# 編程指南)
- 顯式接口實現(C# 編程指南)
- 如何:顯式實現接口成員(C# 編程指南)
- 如何:顯式實現兩個接口的成員(C# 編程指南)
- 互操作性(C# 編程指南)
- 互操作性概述(C# 編程指南)
- 如何:通過使用 Visual C# 功能訪問 Office 互操作對象(C# 編程指南)
- 如何:在 COM 互操作編程中使用索引屬性(C# 編程指南)
- 如何:使用平臺調用播放波形文件(C# 編程指南)
- 演練:Office 編程(C# 和 Visual Basic)
- COM 類示例(C# 編程指南)
- LINQ 查詢表達式(C# 編程指南)
- 查詢表達式基礎(C# 編程指南)
- 如何:在 C# 中編寫 LINQ 查詢
- 如何:查詢對象集合(C# 編程指南)
- 如何:從方法中返回查詢(C# 編程指南)
- 如何:在內存中存儲查詢結果(C# 編程指南)
- 如何:對查詢結果進行分組(C# 編程指南)
- 如何:創建嵌套組(C# 編程指南)
- 如何:對分組操作執行子查詢(C# 編程指南)
- 如何:按連續鍵對結果進行分組(C# 編程指南)
- 如何:在運行時動態指定謂詞篩選器(C# 編程指南)
- 如何:執行內部聯接(C# 編程指南)
- 如何:執行分組聯接(C# 編程指南)
- 如何:執行左外部聯接(C# 編程指南)
- 如何:對 Join 子句的結果進行排序(C# 編程指南)
- 如何:使用復合鍵進行聯接(C# 編程指南)
- 如何:執行自定義聯接操作(C# 編程指南)
- 如何:在查詢表達式中處理 Null 值(C# 編程指南)
- 如何:在查詢表達式中處理異常(C# 編程指南)
- Main() 和命令行參數(C# 編程指南)
- 命令行參數(C# 編程指南)
- 如何:顯示命令行參數(C# 編程指南)
- 如何:使用 foreach 訪問命令行參數(C# 編程指南)
- Main() 返回值(C# 編程指南)
- 命名空間(C# 編程指南)
- 使用命名空間(C# 編程指南)
- 如何:使用全局命名空間別名(C# 編程指南)
- 如何:使用 My 命名空間(C# 編程指南)
- 可以為 null 的類型(C# 編程指南)
- 使用可以為 null 的類型(C# 編程指南)
- 裝箱可以為 null 的類型(C# 編程指南)
- 如何:標識可以為 null 的類型(C# 編程指南)
- 如何:安全地將 bool? 強制轉換為 bool(C# 編程指南)
- 語句、表達式和運算符(C# 編程指南)
- 語句(C# 編程指南)
- 表達式(C# 編程指南)
- 運算符(C# 編程指南)
- 匿名函數(C# 編程指南)
- 可重載運算符(C# 編程指南)
- 轉換運算符(C# 編程指南)
- 如何:使用運算符重載創建復數類(C# 編程指南)
- 相等比較(C# 編程指南)
- 字符串(C# 編程指南)
- 如何:串聯多個字符串(C# 編程指南)
- 如何:修改字符串內容(C# 編程指南)
- 如何:比較字符串(C# 編程指南)
- 如何:拆分字符串(C# 編程指南)
- 如何:使用字符串方法搜索字符串(C# 編程指南)
- 如何:使用正則表達式搜索字符串(C# 編程指南)
- 如何:確定字符串是否表示數值(C# 編程指南)
- 如何:將字符串轉換為 DateTime(C# 編程指南)
- 如何:在舊式編碼與 Unicode 之間轉換(C# 編程指南)
- 如何:將 RTF 轉換為純文本(C# 編程指南)
- 類型(C# 編程指南)
- 強制轉換和類型轉換(C# 編程指南)
- 裝箱和取消裝箱(C# 編程指南)
- 使用類型 dynamic(C# 編程指南)
- 如何:使用 as 和 is 運算符安全地進行強制轉換(C# 編程指南)
- 如何:將字節數組轉換為 int(C# 編程指南)
- 如何:將字符串轉換為數字(C# 編程指南)
- 如何:在十六進制字符串與數值類型之間轉換(C# 編程指南)
- 不安全代碼和指針(C# 編程指南)
- 固定大小的緩沖區(C# 編程指南)
- 指針類型(C# 編程指南)
- XML 文檔注釋(C# 編程指南)
- 建議的文檔注釋標記(C# 編程指南)
- 處理 XML 文件(C# 編程指南)
- 文檔標記的分隔符(C# 編程指南)
- 如何:使用 XML 文檔功能(C# 編程指南)
- C# 參考
- C# 關鍵字
- 類型(C# 參考)
- 值類型(C# 參考)
- bool(C# 參考)
- byte(C# 參考)
- char(C# 參考)
- decimal(C# 參考)
- double(C# 參考)
- enum(C# 參考)
- float(C# 參考)
- int(C# 參考)
- long(C# 參考)
- sbyte(C# 參考)
- short(C# 參考)
- struct(C# 參考)
- uint(C# 參考)
- ulong(C# 參考)
- ushort(C# 參考)
- 引用類型(C# 參考)
- class(C# 參考)
- 委托(C# 參考)
- dynamic(C# 參考)
- 接口(C# 參考)
- object(C# 參考)
- string(C# 參考)
- 內插字符串(C# 和 Visual Basic 引用)
- void(C# 參考)
- var(C# 參考)
- 類型參考表(C# 參考)
- 內置類型表(C# 參考)
- 整型表(C# 參考)
- 浮點型表(C# 參考)
- 默認值表(C# 參考)
- 值類型表(C# 參考)
- 隱式數值轉換表(C# 參考)
- 顯式數值轉換表(C# 參考)
- 設置數值結果表的格式(C# 參考)
- 修飾符(C# 參考)
- 訪問修飾符(C# 參考)
- 可訪問性級別(C# 參考)
- 可訪問域(C# 參考)
- 可訪問性級別的使用限制(C# 參考)
- internal(C# 參考)
- private(C# 參考)
- protected(C# 參考)
- public(C# 參考)
- abstract(C# 參考)
- async(C# 參考)
- const(C# 參考)
- event(C# 參考)
- extern(C# 參考)
- in(泛型修飾符)(C# 參考)
- out(泛型修飾符)(C# 參考)
- override(C# 參考)
- readonly(C# 參考)
- sealed(C# 參考)
- static(C# 參考)
- unsafe(C# 參考)
- virtual(C# 參考)
- volatile(C# 參考)
- 語句關鍵字(C# 參考)
- 選擇語句(C# 參考)
- if-else(C# 參考)
- switch(C# 參考)
- 迭代語句(C# 參考)
- do(C# 參考)
- for(C# 參考)
- foreach,in(C# 參考)
- while(C# 參考)
- 跳轉語句(C# 參考)
- break(C# 參考)
- continue(C# 參考)
- goto(C# 參考)
- return(C# 參考)
- 異常處理語句(C# 參考)
- throw(C# 參考)
- try-catch(C# 參考)
- try-finally(C# 參考)
- try-catch-finally(C# 參考)
- Checked 和 Unchecked(C# 參考)
- checked(C# 參考)
- unchecked(C# 參考)
- fixed 語句(C# 參考)
- “鎖定”語句(C# 參考)
- 方法參數(C# 參考)
- params(C# 參考)
- ref(C# 參考)
- out(C# 參考)
- out 參數修飾符(C# 參考)
- 命名空間關鍵字(C# 參考)
- 命名空間(C# 參考)
- using(C# 參考)
- using 指令(C# 參考)
- using 語句(C# 參考)
- 外部別名(C# 參考)
- 運算符關鍵字(C# 參考)
- as(C# 參考)
- await(C# 參考)
- is(C# 參考)
- new(C# 參考)
- new 運算符(C# 參考)
- new 修飾符(C# 參考)
- new 約束(C# 參考)
- sizeof(C# 參考)
- typeof(C# 參考)
- true(C# 參考)
- true 運算符(C# 參考)
- true 字面常數(C# 參考)
- false(C# 參考)
- false 運算符(C# 參考)
- false 字面常數(C# 參考)
- stackalloc(C# 參考)
- nameof(C# 和 Visual Basic 引用)
- 轉換關鍵字(C# 參考)
- explicit(C# 參考)
- implicit(C# 參考)
- 運算符(C# 參考)
- 訪問關鍵字(C# 參考)
- base(C# 參考)
- this(C# 參考)
- 文字關鍵字(C# 參考)
- null(C# 參考)
- default(C# 參考)
- 上下文關鍵字(C# 參考)
- add(C# 參考)
- get(C# 參考)
- global(C# 參考)
- 分部(類型)(C# 參考)
- 分部(方法)(C# 參考)
- remove(C# 參考)
- set(C# 參考)
- where(泛型類型約束)(C# 參考)
- value(C# 參考)
- yield(C# 參考)
- 查詢關鍵字(C# 參考)
- from 子句(C# 參考)
- where 子句(C# 參考)
- select 子句(C# 參考)
- group 子句(C# 參考)
- into(C# 參考)
- orderby 子句(C# 參考)
- join 子句(C# 參考)
- let 子句(C# 參考)
- ascending(C# 參考)
- descending(C# 參考)
- on(C# 參考)
- equals(C# 參考)
- by(C# 參考)
- in(C# 參考)
- C# 運算符
- 運算符(C# 參考)
- () 運算符(C# 參考)
- . 運算符(C# 參考)
- :: 運算符(C# 參考)
- + 運算符(C# 參考)
- - 運算符(C# 參考)
- * 運算符(C# 參考)
- / 運算符(C# 參考)
- % 運算符(C# 參考)
- & 運算符(C# 參考)
- | 運算符(C# 參考)
- ^ 運算符(C# 參考)
- ! 運算符(C# 參考)
- ~ 運算符(C# 參考)
- = 運算符(C# 參考)
- &lt; 運算符(C# 參考)
- &gt; 運算符(C# 參考)
- ?: 運算符(C# 參考)
- ++ 運算符(C# 參考)
- -- 運算符(C# 參考)
- && 運算符(C# 參考)
- || 運算符(C# 參考)
- &lt;&lt; 運算符(C# 參考)
- &gt;&gt; 運算符(C# 參考)
- == 運算符(C# 參考)
- != 運算符(C# 參考)
- &lt;= 運算符(C# 參考)
- &gt;= 運算符(C# 參考)
- += 運算符(C# 參考)
- -= 運算符(C# 參考)
- *= 運算符(C# 參考)
- /= 運算符(C# 參考)
- %= 運算符(C# 參考)
- &= 運算符(C# 參考)
- |= 運算符(C# 參考)
- ^= 運算符(C# 參考)
- &lt;&lt;= 運算符(C# 參考)
- &gt;&gt;= 運算符(C# 參考)
- -&gt; 運算符(C# 參考)
- ?? 運算符(C# 參考)
- =&gt; 運算符(C# 參考)
- NULL 條件運算符(C# 和 Visual Basic)
- C# 預處理器指令
- #if(C# 參考)
- #else(C# 參考)
- #elif(C# 參考)
- #endif(C# 參考)
- #define(C# 參考)
- #undef(C# 參考)
- #warning(C# 參考)
- #error(C# 參考)
- #line(C# 參考)
- #region(C# 參考)
- #endregion(C# 參考)
- #pragma(C# 參考)
- #pragma warning(C# 參考)
- #pragma checksum(C# 參考)
- C# Compiler Options
- Command-line Building With csc.exe
- How to: Set Environment Variables for the Visual Studio Command Line
- Deployment of C# Applications
- C# Compiler Options Listed by Category
- C# Compiler Options Listed Alphabetically
- @ (C# Compiler Options)
- /addmodule (C# Compiler Options)
- /appconfig (C# Compiler Options)
- /baseaddress (C# Compiler Options)
- /bugreport (C# Compiler Options)
- /checked (C# Compiler Options)
- /codepage (C# Compiler Options)
- /debug (C# Compiler Options)
- /define (C# Compiler Options)
- /delaysign (C# Compiler Options)
- /doc (C# Compiler Options)
- /errorreport (C# Compiler Options)
- /filealign (C# Compiler Options)
- /fullpaths (C# Compiler Options)
- /help, /? (C# Compiler Options)
- /highentropyva (C# Compiler Options)
- /keycontainer (C# Compiler Options)
- /keyfile (C# Compiler Options)
- /langversion (C# Compiler Options)
- /lib (C# Compiler Options)
- /link (C# Compiler Options)
- /linkresource (C# Compiler Options)
- /main (C# Compiler Options)
- /moduleassemblyname (C# Compiler Option)
- /noconfig (C# Compiler Options)
- /nologo (C# Compiler Options)
- /nostdlib (C# Compiler Options)
- /nowarn (C# Compiler Options)
- /nowin32manifest (C# Compiler Options)
- /optimize (C# Compiler Options)
- /out (C# Compiler Options)
- /pdb (C# Compiler Options)
- /platform (C# Compiler Options)
- /preferreduilang (C# Compiler Options)
- /recurse (C# Compiler Options)
- /reference (C# Compiler Options)
- /resource (C# Compiler Options)
- /subsystemversion (C# Compiler Options)
- /target (C# Compiler Options)
- /target:appcontainerexe(C# 編譯器選項)
- /target:exe (C# Compiler Options)
- /target:library (C# Compiler Options)
- /target:module (C# Compiler Options)
- /target:winexe (C# Compiler Options)
- /target:winmdobj(C# 編譯器選項)
- /unsafe (C# Compiler Options)
- /utf8output (C# Compiler Options)
- /warn (C# Compiler Options)
- /warnaserror (C# Compiler Options)
- /win32icon (C# Compiler Options)
- /win32manifest (C# Compiler Options)
- /win32res (C# Compiler Options)
- C# Compiler Errors
- Compiler Error CS0001
- Compiler Error CS0006
- Compiler Error CS0007
- 編譯器錯誤 CS0015
- Compiler Error CS0016
- Compiler Error CS0019
- Compiler Error CS0029
- Compiler Error CS0034
- Compiler Error CS0038
- Compiler Error CS0039
- Compiler Error CS0050
- Compiler Error CS0051
- Compiler Error CS0052
- Compiler Error CS0071
- Compiler Error CS0103
- Compiler Error CS0106
- Compiler Error CS0115
- Compiler Error CS0116
- Compiler Error CS0120
- Compiler Error CS0122
- Compiler Error CS0134
- Compiler Error CS0151
- 編譯器錯誤 CS0163
- Compiler Error CS0165
- Compiler Error CS0173
- Compiler Error CS0178
- Compiler Error CS0188
- Compiler Error CS0201
- Compiler Error CS0229
- Compiler Error CS0233
- Compiler Error CS0234
- Compiler Error CS0246
- Compiler Error CS0260
- Compiler Error CS0266
- Compiler Error CS0269
- Compiler Error CS0270
- Compiler Error CS0304
- Compiler Error CS0310
- Compiler Error CS0311
- Compiler Error CS0413
- Compiler Error CS0417
- Compiler Error CS0433
- Compiler Error CS0445
- Compiler Error CS0446
- Compiler Error CS0504
- 編譯器錯誤 CS0507
- Compiler Error CS0518
- Compiler Error CS0523
- Compiler Error CS0545
- Compiler Error CS0552
- Compiler Error CS0563
- Compiler Error CS0570
- Compiler Error CS0571
- Compiler Error CS0579
- Compiler Error CS0592
- Compiler Error CS0616
- Compiler Error CS0650
- Compiler Error CS0686
- Compiler Error CS0702
- 編譯器錯誤 CS0703
- Compiler Error CS0731
- Compiler Error CS0826
- Compiler Error CS0834
- Compiler Error CS0840
- 編譯器錯誤 CS0843
- Compiler Error CS0845
- Compiler Error CS1001
- Compiler Error CS1009
- Compiler Error CS1018
- Compiler Error CS1019
- Compiler Error CS1026
- Compiler Error CS1029
- Compiler Error CS1061
- Compiler Error CS1112
- 編譯器錯誤 CS1501
- Compiler Error CS1502
- Compiler Error CS1519
- Compiler Error CS1540
- Compiler Error CS1546
- Compiler Error CS1548
- Compiler Error CS1564
- Compiler Error CS1567
- Compiler Error CS1579
- Compiler Error CS1612
- Compiler Error CS1614
- Compiler Error CS1640
- Compiler Error CS1644
- Compiler Error CS1656
- Compiler Error CS1674
- Compiler Error CS1703
- Compiler Error CS1704
- Compiler Error CS1705
- Compiler Error CS1708
- Compiler Error CS1716
- 編譯器錯誤 CS1721
- Compiler Error CS1726
- Compiler Error CS1729
- Compiler Error CS1919
- Compiler Error CS1921
- Compiler Error CS1926
- Compiler Error CS1933
- Compiler Error CS1936
- Compiler Error CS1941
- Compiler Error CS1942
- Compiler Error CS1943
- Compiler Error CS1946
- 編譯器錯誤 CS2032
- Compiler Warning (level 1) CS0420
- Compiler Warning (level 1) CS0465
- Compiler Warning (level 1) CS1058
- Compiler Warning (level 1) CS1060
- Compiler Warning (level 1) CS1598
- Compiler Warning (level 1) CS1607
- Compiler Warning (level 1) CS1616
- Compiler Warning (level 1) CS1658
- Compiler Warning (level 1) CS1683
- Compiler Warning (level 1) CS1685
- Compiler Warning (level 1) CS1690
- Compiler Warning (level 1) CS1691
- Compiler Warning (level 1) CS1699
- Compiler Warning (level 1) CS1762
- Compiler Warning (level 1) CS1956
- Compiler Warning (level 1) CS3003
- Compiler Warning (level 1) CS3007
- Compiler Warning (level 1) CS3009
- 編譯器警告(等級 1)CS4014
- Compiler Warning (level 2) CS0108
- 編譯器警告(等級 2)CS0467
- Compiler Warning (level 2) CS0618
- Compiler Warning (level 2) CS1701
- Compiler Warning (level 3) CS0675
- Compiler Warning (level 3) CS1700
- Compiler Warning (level 4) CS0429
- Compiler Warning (level 4) CS1591
- Compiler Warning (level 4) CS1610
- C# 語言規范