当前位置:网站首页>匿名類型(C# 指南 基礎知識)
匿名類型(C# 指南 基礎知識)
2022-04-23 08:29:00 【風神修羅使】
匿名類型提供了一種方便的方法,可用來將一組只讀屬性封裝到單個對象中,而無需首先顯式定義一個類型。 類型名由編譯器生成,並且不能在源代碼級使用。 每個屬性的類型由編譯器推斷。
可結合使用 new
運算符和對象初始值設定項創建匿名類型。 有關對象初始值設定項的詳細信息,請參閱對象和集合初始值設定項。
以下示例顯示了用兩個名為 Amount
和 Message
的屬性進行初始化的匿名類型。
var v = new {
Amount = 108, Message = "Hello" };
// Rest the mouse pointer over v.Amount and v.Message in the following
// statement to verify that their inferred types are int and string.
Console.WriteLine(v.Amount + v.Message);
匿名類型通常用在查詢錶達式的 select
子句中,以便返回源序列中每個對象的屬性子集。 有關查詢的詳細信息,請參閱C# 中的 LINQ。
匿名類型包含一個或多個公共只讀屬性。 包含其他種類的類成員(如方法或事件)為無效。 用來初始化屬性的錶達式不能為 null
、匿名函數或指針類型。
最常見的方案是用其他類型的屬性初始化匿名類型。 在下面的示例中,假定名為 Product
的類存在。 類 Product
包括 Color
和 Price
屬性,以及你不感興趣的其他屬性。 變量 Productproducts
是 對象的集合。 匿名類型聲明以 new
關鍵字開始。 聲明初始化了一個只使用 Product
的兩個屬性的新類型。 使用匿名類型會導致在查詢中返回的數據量變少。
如果你沒有在匿名類型中指定成員名稱,編譯器會為匿名類型成員指定與用於初始化這些成員的屬性相同的名稱。 需要為使用錶達式初始化的屬性提供名稱,如下面的示例所示。 在下面示例中,匿名類型的屬性名稱都為 PriceColor
和 。
var productQuery =
from prod in products
select new {
prod.Color, prod.Price };
foreach (var v in productQuery)
{
Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}
通常,當使用匿名類型來初始化變量時,可以通過使用 var
將變量作為隱式鍵入的本地變量來進行聲明。 類型名稱無法在變量聲明中給出,因為只有編譯器能訪問匿名類型的基礎名稱。 有關 var
的詳細信息,請參閱var
。
可通過將隱式鍵入的本地變量與隱式鍵入的數組相結合創建匿名鍵入的元素的數組,如下面的示例所示。
var anonArray = new[] {
new {
name = "apple", diam = 4 }, new {
name = "grape", diam = 1 }};
匿名類型是 class
類型,它們直接派生自 object
,並且無法强制轉換為除 object
外的任何類型。 雖然你的應用程序不能訪問它,編譯器還是提供了每一個匿名類型的名稱。 從公共語言運行時的角度來看,匿名類型與任何其他引用類型沒有什麼不同。
如果程序集中的兩個或多個匿名對象初始值指定了屬性序列,這些屬性采用相同順序且具有相同的名稱和類型,則編譯器將對象視為相同類型的實例。 它們共享同一編譯器生成的類型信息。
匿名類型支持采用 with 錶達式
形式的非破壞性修改。 這使你能够創建匿名類型的新實例,其中一個或多個屬性具有新值:
var apple = new {
Item = "apples", Price = 1.35 };
var onSale = apple with {
Price = 0.79 };
Console.WriteLine(apple);
Console.WriteLine(onSale);
無法將字段、屬性、時間或方法的返回類型聲明為具有匿名類型。 同樣,你不能將方法、屬性、構造函數或索引器的形參聲明為具有匿名類型。 要將匿名類型或包含匿名類型的集合作為參數傳遞給某一方法,可將參數作為類型 object
進行聲明。 但是,對匿名類型使用 object
違背了强類型的目的。 如果必須存儲查詢結果或者必須將查詢結果傳遞到方法邊界外部,請考慮使用普通的命名結構或類而不是匿名類型。
由於匿名類型上的 Equals
和 GetHashCode
方法是根據方法屬性的 Equals
和 GetHashCode
定義的,因此僅當同一匿名類型的兩個實例的所有屬性都相等時,這兩個實例才相等。
版权声明
本文为[風神修羅使]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230758281298.html
边栏推荐
- CSV column extract column extraction
- Introduction to protobuf
- MySQL数据库中delete、truncate、drop原理详解
- Let the earth have less "carbon" and rest on the road
- Excle plus watermark
- My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together
- Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
- synchronized 实现原理
- How to generate assembly file
- word加水印
猜你喜欢
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
Install MySQL for Ubuntu and query the average score
ATSS(CVPR2020)
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
项目上传部分
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
excle加水印
数据可视化:使用Excel制作雷达图
随机推荐
Enctype attribute in form
pgsql想实现mysql一样样的列子查询操作
如何保护开源项目免遭供应链攻击-安全设计(1)
JS converts tree structure data into one-dimensional array data
Redis master-slave server problem
Shell脚本进阶
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
【学习】从零开始的音视频开发(9)——NuPlayer
Input / output system
Vowel substring in statistical string of leetcode simple problem
What is RPC
Search the complete navigation program source code
CSV column extract column extraction
QFileDialog select multiple files or folders
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
总线结构概述
作文以记之 ~ 二叉树的前序遍历
Copy array in JS
Queue (C language / linked list)
DOM learning notes - traverse all element nodes of the page