# 查詢表達式基礎(C# 編程指南)
## 什么是查詢?它有什么用途?
“查詢”是指一組指令,這些指令描述要從一個或多個給定數據源檢索的數據以及返回的數據應該使用的格式和組織形式。查詢不同于它所產生的結果。
通常,源數據會在邏輯上組織為相同種類的元素序列。SQL 數據庫表包含一個行序列。與此類似,ADO.NET [DataTable](https://msdn.microsoft.com/zh-cn/library/system.data.datatable.aspx) 包含一個 [DataRow](https://msdn.microsoft.com/zh-cn/library/system.data.datarow.aspx) 對象序列。在 XML 文件中,有一個 XML 元素“序列”(不過這些元素按分層形式組織為樹結構)。內存中的集合包含一個對象序列。
從應用程序的角度來看,原始源數據的具體類型和結構并不重要。應用程序始終將源數據視為一個 [IEnumerable<T>](https://msdn.microsoft.com/zh-cn/library/9eekhta0.aspx) 或 [IQueryable<T>](https://msdn.microsoft.com/zh-cn/library/bb351562.aspx) 集合。在 LINQ to XML 中,源數據顯示為一個 **IEnumerable**<[XElement](https://msdn.microsoft.com/zh-cn/library/system.xml.linq.xelement.aspx)>。在 LINQ to DataSet 中,它是一個 **IEnumerable**<[DataRow](https://msdn.microsoft.com/zh-cn/library/system.data.datarow.aspx)>。在 LINQ to SQL 中,它是您定義用來表示 SQL 表中數據的任何自定義對象的 **IEnumerable** 或 **IQueryable**。
指定此源序列后,查詢可以進行下列三項工作之一:
* 檢索一個元素子集以產生一個新序列,但不修改單個元素。然后,查詢可以按各種方式對返回的序列進行排序或分組,如下面的示例所示(假定 scores 是 **int[]**):
```
IEnumerable<int> highScoresQuery =
from score in scores
where score > 80
orderby score descending
select score;
```
* 如上一個示例所述檢索一個元素序列,但是將這些元素轉換為具有新類型的對象。例如,查詢可以只從數據源中的某些客戶記錄檢索姓氏。或者,查詢可以檢索完整的記錄,再使用它構建另一個內存中對象類型甚至 XML 數據,然后生成最終的結果序列。下面的示例演示了從 **int** 到 **string** 的轉換。請注意 highScoresQuery 的新類型。
```
IEnumerable<string> highScoresQuery2 =
from score in scores
where score > 80
orderby score descending
select String.Format("The score is {0}", score);
```
* 檢索有關源數據的單一值,例如:
* 符合某個條件的元素的數量。
* 具有最大值或最小值的元素。
* 符合某個條件的第一個元素,或一組指定元素中的特定值之和。例如,下面的查詢從 scores 整數數組中返回高于 80 的分數的數量。
```
int highScoreCount =
(from score in scores
where score > 80
select score)
.Count();
```
在上一個示例中,請注意在 **Count** 方法調用之前的查詢表達式兩旁使用了括號。另一種表示方式是使用一個新變量來存儲具體結果。此技術的可讀性更好,因為它將存儲查詢的變量與存儲結果的查詢區分開來。
```
IEnumerable<int> highScoresQuery3 =
from score in scores
where score > 80
select score;
int scoreCount = highScoresQuery3.Count();
```
在上一個示例中,查詢是在 **Count** 調用中執行的,因為 **Count** 必須循環訪問結果以便確定 highScoresQuery 返回的元素數量。
## 什么是查詢表達式?
“查詢表達式”是用查詢語法表示的查詢,是一流的語言構造。它就像任何其他表達式一樣,并且可以用在 C# 表達式有效的任何上下文中。查詢表達式由一組用類似于 SQL 或 XQuery 的聲明性語法編寫的子句組成。每個子句又包含一個或多個 C# 表達式,而這些表達式本身又可能是查詢表達式或包含查詢表達式。
查詢表達式必須以 [from](https://msdn.microsoft.com/zh-cn/library/bb383978.aspx) 子句開頭,并且必須以 [select](https://msdn.microsoft.com/zh-cn/library/bb384087.aspx) 或 [group](https://msdn.microsoft.com/zh-cn/library/bb384063.aspx) 子句結尾。在第一個 **from** 子句和最后一個 **select** 或 **group** 子句之間,查詢表達式可以包含一個或多個下列可選子句:[where](https://msdn.microsoft.com/zh-cn/library/bb311043.aspx)、[orderby](https://msdn.microsoft.com/zh-cn/library/bb383982.aspx)、[join](https://msdn.microsoft.com/zh-cn/library/bb311040.aspx)、[let](https://msdn.microsoft.com/zh-cn/library/bb383976.aspx) 甚至附加的 [from](https://msdn.microsoft.com/zh-cn/library/bb383978.aspx) 子句。還可以使用 [into](https://msdn.microsoft.com/zh-cn/library/bb311045.aspx) 關鍵字使 **join** 或 **group** 子句的結果能夠充當同一查詢表達式中附加查詢子句的源。
### 查詢變量
在 LINQ 中,查詢變量是任何存儲查詢(而非查詢結果)的變量。更具體地說,查詢變量始終是一個可枚舉的類型,當在 **foreach** 語句中或在對其 **IEnumerator.MoveNext** 方法的直接調用中循環訪問它時,它會生成一序列元素。
下面的代碼示例演示了一個簡單的查詢表達式,它含有一個數據源、一個篩選子句和一個排序子句,但不對源元素進行轉換。 **select** 子句結束了該查詢。
```
static void Main()
{
// Data source.
int[] scores = { 90, 71, 82, 93, 75, 82 };
// Query Expression.
IEnumerable<int> scoreQuery = //query variable
from score in scores //required
where score > 80 // optional
orderby score descending // optional
select score; //must end with select or group
// Execute the query to produce the results
foreach (int testScore in scoreQuery)
{
Console.WriteLine(testScore);
}
}
// Outputs: 93 90 82 82
```
在上一個示例中,scoreQuery 是一個查詢變量,有時簡稱為“查詢”。查詢變量并不存儲實際的結果數據(這些數據是在 **foreach** 循環中產生的)。另外,當 **foreach** 語句執行時,查詢結果并不是通過查詢變量 scoreQuery 返回的。相反,它們是通過迭代變量 testScore 返回的。可以在另一個 **foreach** 循環中迭代 scoreQuery 變量。只要該變量和數據源都沒有修改,該變量都將產生相同的結果。
查詢變量可以存儲用查詢語法或方法語法(或二者的組合)表示的查詢。在下面的示例中,queryMajorCities 和 queryMajorCities2 都是查詢變量:
```
//Query syntax
IEnumerable<City> queryMajorCities =
from city in cities
where city.Population > 100000
select city;
// Method-based syntax
IEnumerable<City> queryMajorCities2 = cities.Where(c => c.Population > 100000);
```
另一方面,下面的兩個示例演示了不是查詢變量的變量,即使每個變量都用查詢進行了初始化。它們不是查詢變量的原因是它們存儲了結果:
```
int highestScore =
(from score in scores
select score)
.Max();
// or split the expression
IEnumerable<int> scoreQuery =
from score in scores
select score;
int highScore = scoreQuery.Max();
List<City> largeCitiesList =
(from country in countries
from city in country.Cities
where city.Population > 10000
select city)
.ToList();
// or split the expression
IEnumerable<City> largeCitiesQuery =
from country in countries
from city in country.Cities
where city.Population > 10000
select city;
List<City> largeCitiesList2 = largeCitiesQuery.ToList();
```
|  注意 |
| :-- |
| 在 LINQ 文檔中,存儲查詢的變量在其名稱中包含單詞“query”,而存儲實際結果的變量在其名稱中不包含單詞“query”。 |
有關用于表示查詢的不同方式的更多信息,請參見[Query Syntax and Method Syntax in LINQ (C#)](https://msdn.microsoft.com/zh-cn/library/bb397947.aspx)。
#### 查詢變量的顯式類型化和隱式類型化
本文檔通常提供查詢變量的顯式類型,以便演示查詢變量和 [select 子句](https://msdn.microsoft.com/zh-cn/library/bb384087.aspx)之間的類型關系。但是,也可以使用 [var](https://msdn.microsoft.com/zh-cn/library/bb383973.aspx) 關鍵字指示編譯器在編譯時推斷查詢變量(或任何其他本地變量)的類型。例如,還可以使用隱式類型化表示本主題前面部分中演示的查詢示例:
```
// Use of var is optional here and in all queries.
// queryCities is an IEnumerable<City> just as
// when it is explicitly typed.
var queryCities =
from city in cities
where city.Population > 100000
select city;
```
有關更多信息,請參見[隱式類型的局部變量(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb384061.aspx)和 [Type Relationships in LINQ Query Operations (C#)](https://msdn.microsoft.com/zh-cn/library/bb397924.aspx)。
### 開始查詢表達式
查詢表達式必須以 **from** 子句開頭。它同時指定了數據源和范圍變量。在對源序列進行遍歷的過程中,范圍變量表示源序列中的每個后續元素。將根據數據源中元素的類型對范圍變量進行強類型化。在下面的示例中,因為 countries 是 Country 對象數組,所以范圍變量也被類型化為 Country,這樣就可以使用點運算符來訪問該類型的任何可用成員。
```
IEnumerable<Country> countryAreaQuery =
from country in countries
where country.Area > 500000 //sq km
select country;
```
在使用分號或延續子句退出查詢之前,范圍變量將一直位于范圍中。
查詢表達式可以包含多個 **from** 子句。當源序列中的每個元素本身就是集合或包含集合時,可使用附加的 **from** 子句。例如,假定您具有一個 Country 對象集合,而其中每個對象都包含一個名為 Cities 的 City 對象集合。若要查詢每個 Country 中的 City 對象,請使用兩個**from** 子句,如下所示:
```
IEnumerable<City> cityQuery =
from country in countries
from city in country.Cities
where city.Population > 10000
select city;
```
有關更多信息,請參見 [from 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb383978.aspx)。
### 結束查詢表達式
查詢表達式必須以 **select** 子句或 **group** 子句結尾。
#### group 子句
使用 **group** 子句可產生按照指定的鍵組織的組序列。鍵可以采用任何數據類型。例如,下面的查詢創建一個組序列,該序列包含一個或多個 Country 對象,并且它的鍵是 **char** 值。
```
var queryCountryGroups =
from country in countries
group country by country.Name[0];
```
有關分組的更多信息,請參見 [group 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb384063.aspx)。
#### select 子句
使用 **select** 子句可產生所有其他類型的序列。簡單的 **select** 子句只是產生與數據源中包含的對象具有相同類型的對象的序列。在此示例中,數據源包含 Country 對象。 **orderby** 子句只是將元素重新排序,而 **select** 子句則產生重新排序的 Country 對象的序列。
```
IEnumerable<Country> sortedQuery =
from country in countries
orderby country.Area
select country;
```
可以使用 **select** 子句將源數據轉換為新類型的序列。這一轉換也稱為“投影”。在下面的示例中,**select** 子句對一個匿名類型序列進行投影,該序列僅包含原始元素中各字段的子集。請注意,新對象是使用對象初始值設定項初始化的。
```
// Here var is required because the query
// produces an anonymous type.
var queryNameAndPop =
from country in countries
select new { Name = country.Name, Pop = country.Population };
```
有關使用 **select** 子句轉換源數據的所有方式的更多信息,請參見 [select 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb384087.aspx)。
#### 使用“into”進行延續
可以在 **select** 或 **group** 子句中使用 **into** 關鍵字來創建用于存儲查詢的臨時標識符。當您必須在分組或選擇操作之后對查詢執行附加查詢操作時,需要這樣做。在下面的示例中,以一千萬人口范圍為界對 countries 進行分組。在創建這些組之后,使用附加子句篩選掉某些組,然后按升序對剩下的組進行排序。若要執行這些附加操作,需要使用由 countryGroup 表示的延續。
```
// percentileQuery is an IEnumerable<IGrouping<int, Country>>
var percentileQuery =
from country in countries
let percentile = (int) country.Population / 10000000
group country by percentile into countryGroup
where countryGroup.Key >= 20
orderby countryGroup.Key
select countryGroup;
// grouping is an IGrouping<int, Country>
foreach (var grouping in percentileQuery)
{
Console.WriteLine(grouping.Key);
foreach (var country in grouping)
Console.WriteLine(country.Name + ":" + country.Population);
}
```
有關更多信息,請參見 [into(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb311045.aspx)。
### 篩選、排序和聯接
在 **from** 開始子句以及 **select** 或 **group** 結束子句之間,所有其他子句(**where**、**join**、**orderby**、**from**、**let**)都是可選的。任何可選子句都可以在查詢正文中使用零次或多次。
#### where 子句
使用 **where** 子句可以根據一個或多個謂詞表達式篩選掉源數據中的某些元素。以下示例中的 **where** 子句含有兩個謂詞。
```
IEnumerable<City> queryCityPop =
from city in cities
where city.Population < 200000 && city.Population > 100000
select city;
```
有關更多信息,請參見 [where 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb311043.aspx)。
#### orderby 子句
使用 **orderby** 子句可以按升序或降序對結果進行排序。您還可以指定次要排序順序。下面的示例使用 Area 屬性對 country 對象執行主要排序,然后使用 Population 屬性執行次要排序。
```
IEnumerable<Country> querySortedCountries =
from country in countries
orderby country.Area, country.Population descending
select country;
```
**ascending** 關鍵字是可選的;如果未指定順序,則它是默認排序順序。有關更多信息,請參見 [orderby 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb383982.aspx)。
#### join 子句
使用 **join** 子句可以根據每個元素中指定鍵之間的相等比較,對一個數據源中的元素與另外一個數據源中的元素進行關聯和/或組合。在 LINQ 中,聯接操作是針對其元素具有不同類型的對象序列執行的。在聯接兩個序列之后,必須使用 **select** 或 **group** 語句指定要存儲到輸出序列中的元素。還可以使用匿名類型將每組關聯元素中的屬性組合為輸出序列的新類型。下面的示例對其 Category 屬性與 categories 字符串數組中的某個類別相匹配的 prod 對象進行關聯。其 Category 不與 categories 中的任何字符串匹配的產品會被篩選掉。 **select** 語句投影了一個新類型,其屬性取自 cat 和 prod。
```
var categoryQuery =
from cat in categories
join prod in products on cat equals prod.Category
select new { Category = cat, Name = prod.Name };
```
通過使用 [into](https://msdn.microsoft.com/zh-cn/library/bb311045.aspx) 關鍵字將 **join** 操作的結果存儲到臨時變量中,還可以執行分組聯接。有關更多信息,請參見 [join 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb311040.aspx)。
#### let 子句
使用 **let** 子句可以將表達式(如方法調用)的結果存儲到新的范圍變量中。在下面的示例中,范圍變量 firstName 存儲了 Split 返回的字符串數組的第一個元素。
```
string[] names = { "Svetlana Omelchenko", "Claire O'Donnell", "Sven Mortensen", "Cesar Garcia" };
IEnumerable<string> queryFirstNames =
from name in names
let firstName = name.Split(new char[] { ' ' })[0]
select firstName;
foreach (string s in queryFirstNames)
Console.Write(s + " ");
//Output: Svetlana Claire Sven Cesar
```
有關更多信息,請參見 [let 子句(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb383976.aspx)。
### 查詢表達式中的子查詢
查詢子句本身可能包含一個查詢表達式,該查詢表達式有時稱為“子查詢”。每個子查詢都以它自己的 **from** 子句開頭,該子句不一定指向第一個 **from** 子句中的同一數據源。例如,下面的查詢演示了一個在 select 語句中使用的查詢表達式,用來檢索分組操作的結果。
```
var queryGroupMax =
from student in students
group student by student.GradeLevel into studentGroup
select new
{
Level = studentGroup.Key,
HighestScore =
(from student2 in studentGroup
select student2.Scores.Average())
.Max()
};
```
有關更多信息,請參見 [如何:對分組操作執行子查詢(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb311041.aspx)。
## 請參閱
[C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx)
[LINQ 查詢表達式(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/bb397676.aspx)
[LINQ (Language-Integrated Query)](https://msdn.microsoft.com/zh-cn/library/bb397926.aspx)
[查詢關鍵字(C# 參考)](https://msdn.microsoft.com/zh-cn/library/bb310804.aspx)
[Standard Query Operators Overview](https://msdn.microsoft.com/zh-cn/library/bb397896.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# 語言規范