当前位置:网站首页>Anonymous type (c Guide Basics)
Anonymous type (c Guide Basics)
2022-04-23 08:30:00 【Wind god Shura envoy】
Anonymous types provide a convenient way , Can be used to encapsulate a set of read-only properties into a single object , Instead of explicitly defining a type first . The type name is generated by the compiler , And it can't be used at the source level . The type of each property is inferred by the compiler .
Combined use new Operators and object initializers create anonymous types . For more information about object initializers , See object and collection initializers .
The following example shows two named Amount and Message An anonymous type that initializes the properties of .
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);
Anonymous types are often used in the of query expressions select clause , To return a subset of the attributes of each object in the source sequence . More about queries , see also C# Medium LINQ.
Anonymous types contain one or more public read-only properties . Contains other kinds of class members ( Such as method or event ) Is invalid . The expression used to initialize the property cannot be null、 Anonymous function or pointer type .
The most common solution is to initialize anonymous types with other types of properties . In the following example , Suppose it's called Product Class exists . class Product Include Color and Price attribute , And other attributes you're not interested in . Variable Productproducts yes A collection of objects . Anonymous type declaration with new Keyword start . The declaration initializes a that uses only Product New types of two properties of . Using anonymous types will result in less data returned in the query .
If you don't specify a member name in an anonymous type , The compiler assigns anonymous type members the same name as the properties used to initialize them . You need to provide a name for the property initialized with an expression , As the following example shows . In the following example , The attribute names of anonymous types are PriceColor and .
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);
}
Usually , When using anonymous types to initialize variables , By using var Declare variables as implicitly typed local variables . Type name cannot be given in variable declaration , Because only the compiler can access the base name of anonymous types . of var Details of , see also var.
You can create an array of anonymous typed elements by combining implicitly typed local variables with implicitly typed arrays , As the following example shows .
var anonArray = new[] {
new {
name = "apple", diam = 4 }, new {
name = "grape", diam = 1 }};
The anonymous type is class type , They are derived directly from object, And cannot be cast to division object Any type other than . Although your application can't access it , The compiler also provides the name of each anonymous type . From the perspective of the common language runtime , Anonymous types are no different from any other reference type .
If the initial value of two or more anonymous objects in the assembly specifies a sequence of properties , These attributes are in the same order and have the same name and type , The compiler treats the object as an instance of the same type . They share type information generated by the same compiler .
Anonymous types support with expression Non destructive modification of form . This allows you to create new instances of anonymous types , One or more of these properties have new values :
var apple = new {
Item = "apples", Price = 1.35 };
var onSale = apple with {
Price = 0.79 };
Console.WriteLine(apple);
Console.WriteLine(onSale);
Cannot add field 、 attribute 、 The return type of a time or method is declared to have an anonymous type . Again , You can't put the method 、 attribute 、 A formal parameter of a constructor or indexer is declared as having an anonymous type . To pass anonymous types or collections containing anonymous types as parameters to a method , Parameters can be used as types object Make a statement . however , Use... For anonymous types object Against the purpose of strong typing . If the query result must be stored or passed outside the method boundary , Consider using normal naming structures or classes instead of anonymous types .
Because of... On anonymous types Equals and GetHashCode Methods are based on method properties Equals and GetHashCode Defined , Therefore, only if all properties of two instances of the same anonymous type are equal , These two instances are equal .
版权声明
本文为[Wind god Shura envoy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230758281298.html
边栏推荐
- 英语课小记(四)
- pdf加水印
- pgsql想实现mysql一样样的列子查询操作
- 匿名类型(C# 指南 基础知识)
- 对OutputStream类的flush()方法的误解
- [learning] audio and video development from scratch (9) -- nuplayer
- Transformer XL: attention language modelsbbeyond a fixed length context paper summary
- Situational leaders - Chapter 7, solving performance problems
- 396. Rotate Function
- Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
猜你喜欢

synchronized 实现原理

Qtablewidget header customization and beautification developed by pyqt5 (with source code download)

程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法

RPC procedure

Using qlst excel file

QT compilation qtxlsx Library

5.6 comprehensive case - RTU-

How to read books and papers
![[learning] audio and video development from scratch (9) -- nuplayer](/img/62/20b0d80088181fb6ff1fe842500c0a.png)
[learning] audio and video development from scratch (9) -- nuplayer

监控智能回放是什么,如何使用智能回放查询录像
随机推荐
对li类数组对象随机添加特性,并进行排序
Shell script advanced
vslam PPT
Input / output system
Rotation function of leetcode medium problem
CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American
基于TCP/IP协议的网络通信实例——文件传输
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
word加水印
Goland 调试go使用-大白记录
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
Rearranging log files for leetcode simple question
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
【路科V0】验证环境2——验证环境组件
Online app resource download website source code
stm32以及freertos 堆栈解析
K210学习笔记(二) K210与STM32进行串口通信