当前位置:网站首页>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
边栏推荐
- 5.6 综合案例-RTU-
- QFileDialog 选择多个文件或文件夹
- The third divisor of leetcode simple question
- 关于ORB——SLAM运行中关键帧位置越来越近的异常说明
- ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
- jsp页面编码
- 2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry
- An idea plug-in that doesn't work, but can install X
- Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
- 耳穴减肥自身感受细节描述0422
猜你喜欢
监控智能回放是什么,如何使用智能回放查询录像
SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
5.6 综合案例-RTU-
396. Rotate Function
RPC过程
ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
输入/输出系统
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
The simple problem of leetcode is to calculate the numerical sum of strings
随机推荐
Common regular expressions
线程的调度(优先级)
Navicat远程连接mysql
npm安装yarn
【路科V0】验证环境2——验证环境组件
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
396. Rotate Function
DOM学习笔记---遍历页面所有元素节点
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
一键清理项目下pycharm和Jupyter缓存文件
Overview of bus structure
An example of network communication based on TCP / IP protocol -- file transmission
idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
获取TrustedInstaller权限
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
[C语言] 文件操作《一》
耳穴减肥自身感受细节描述0422
Idea: export Yapi interface using easyyapi plug-in