当前位置:网站首页>LINQ学习系列-----1.4 匿名对象
LINQ学习系列-----1.4 匿名对象
2022-04-23 07:58:00 【风神修罗使】
本篇内容接着上一篇继续讲述,本篇简单讲解匿名对象
一.匿名对象介绍
上代码:
var result=new {
ID=1,
Name="张三",
Age=23
};
若想输出这个新对象的Age
属性的内容,可以让Console.WriteLine
深入到该对象的下一层,Console.WriteLine(reuslt,1)
即可
虽然匿名对象没有对象名,不过仍然是类型—编译器将自动为其生成名字。代码的result
将指向上述自动创建的类型的一个实例。该类型有三个属性:ID,Name,Age
。这三个属性是根据初始化器中的声明而推断得出的。
二.匿名对象属性一致的问题
var result1=new {
ID=2,Name="李四",Age=33};
var result2=new {
ID=3,Name="张三",Age=11};
var result3=new {
ID=3,Name="张三"};"
上述代码中result1
和result2
属于同一个类型的两个不同的实例。但是result3
和result1
、result2
是属于不同的类型,因为属性不一致。
注意:上述代码中如果属性的顺序不一致,导致的类型 也是不一致的。
三.匿名对象的限制
- 使用匿名类型时,离开定义该类型的方法,我们将无法再以强类型的方式使用此匿名类型的实例。若要使在此方法内,我们希望将某个匿名类型的实例传递给某一方法的话,被传递的方法的这个参数的类型必须是
object
. - 在定义匿名类型的方法之外,我们只能使用反射来进一步操作该类型的实例。
- 定义该匿名类型的方法的返回值类型必须是
object
,否则匿名类型将不能够作为方法的返回值。 - 匿名类型的实例时不可变的,一旦创建了一个匿名类型的实例,那么该实例的各个属性值就将永远被确定下来了,所有的属性只能get,不能set。
四.匿名对象的不可变特性
因为匿名类型具有不可变的特性,所以其所有的实例都有稳定的散列值(hash code
)。
匿名对象不可变的目的:永远不会改变的对象能够大大降低程序设计中对并发控制的处理难度。这在后续的PLINQ
中起着非常关键的作用。(在某个对象在某个时刻的快照的时候无需考虑其可能带来的不一致的副作用)。
版权声明
本文为[风神修罗使]所创,转载请带上原文链接,感谢
https://blog.csdn.net/WuLex/article/details/123282728
边栏推荐
- vslam PPT
- QFileDialog select multiple files or folders
- Overview of bus structure
- ELK生产实践
- 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
- Campus transfer second-hand market source code download
- DOM学习笔记---遍历页面所有元素节点
- ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
- vmware 搭建ES8的常见错误
- 怎么读书读论文
猜你喜欢
Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
Knowledge points and problem solutions related to information collection
ASAN 极简原理
Kubernetes in browser and IDE | interactive learning platform killercoda
程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法
QT compilation qtxlsx Library
[learning] audio and video development from scratch (9) -- nuplayer
Rearranging log files for leetcode simple question
CSV column extract column extraction
随机推荐
Install MySQL for Ubuntu and query the average score
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
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
An article understands variable lifting
LeetCode-199-二叉树的右视图
DOM 学习之—添加+-按钮
Qt编译QtXlsx库
vmware 搭建ES8的常见错误
Redis master-slave server problem
Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi
AQS & ReentrantLock 实现原理
Detailed description of self feeling of auricular point weight loss 0422
SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
JS common array methods
[effective go Chinese translation] part I
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
ELK生产实践
396. Rotate Function
form表单 post提交 数据量大的问题