当前位置:网站首页>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
边栏推荐
- Using qlst excel file
- How to read books and papers
- 【路科V0】验证环境2——验证环境组件
- 线程的调度(优先级)
- clang 如何产生汇编文件
- Search the complete navigation program source code
- 程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法
- input元素添加监听事件
- 《深度学习》学习笔记(八)
- QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
猜你喜欢

The simple problem of leetcode is to calculate the numerical sum of strings

The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface

2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局

数据可视化:使用Excel制作雷达图

AQS & ReentrantLock 实现原理

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

Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast

396. Rotate Function
![[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel](/img/7c/0adc0940b6d5c8a61d34bfa5f66ee7.png)
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel

【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
随机推荐
CGM optimizes blood glucose monitoring and management -- Yiyu technology appears in Sichuan International Medical Exchange Promotion Association
数论求a^b(a,b为1e12级别)的因子之和
DOM learning - add + - button
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
ajax防止缓存方法
Misunderstanding of flush () method of OutputStream class
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
MySQL数据库中delete、truncate、drop原理详解
作文以记之 ~ 二叉树的后序遍历
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
Detailed description of self feeling of auricular point weight loss 0422
How browser works
[learning] audio and video development from scratch (9) -- nuplayer
clang 如何产生汇编文件
Qt读写XML文件
获取TrustedInstaller权限
耳穴减肥自身感受细节描述0422
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
监控智能回放是什么,如何使用智能回放查询录像
[effective go Chinese translation] part I