惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

博客园 - 赶路人之刚出发

集成WebSecurity的Authorize进行身份验证时,数据库连接报错问题 Html.ActionLink传递参数 Automapper结合EF实现insert,update方法 MVC中使用RemoteAttribute异步远程验证 Html.RenderPartial WebMatrix.WebSecurity创建自定义用户属性 强类型view中List<Model〉问题 ViewBag任意属性的实现方法 params关键字 配置LINQ中的datacontext的log路径,以记录datacontext执行了的查询sql SortedList LINQ join/left join/cross join/group by/group join/sortedlist/cast Linq to objects示例 yield return 和 Func Lamda表达式 IDisposable 对象初始化器和集合初始化器 C#自动属性 .net random伪随机数
匿名类型与扩展方法
赶路人之刚出发 · 2013-04-11 · via 博客园 - 赶路人之刚出发

匿名类型

匿名类型就是没有名字的类型。在C#3.0中允许我们在程序中声明一个临时的类型来存储数据,例如:

var noname = new { name = "Jerry", age = 10 };

编译器编译后的反编译出来的代码为:

View Code

[DebuggerDisplay(@"\{ name = {name}, age = {age} }", Type="<Anonymous Type>"), CompilerGenerated]
internal sealed class <>f__AnonymousType0<<name>j__TPar, <age>j__TPar>
{
    // Fields
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private readonly <age>j__TPar <age>i__Field;
    [DebuggerBrowsable(DebuggerBrowsableState.Never)]
    private readonly <name>j__TPar <name>i__Field;

    // Methods
    [DebuggerHidden]
    public <>f__AnonymousType0(<name>j__TPar name, <age>j__TPar age);
    [DebuggerHidden]
    public override bool Equals(object value);
    [DebuggerHidden]
    public override int GetHashCode();
    [DebuggerHidden]
    public override string ToString();

    // Properties
    public <age>j__TPar age { get; }
    public <name>j__TPar name { get; }
}

其实就是用对象初始化器的做法初始化一个属性都为只读的匿名类型。

在MSDN 中匿名类型的定义是这样的:

   1.匿名类型提供了一种方便的方法,可用来将一组只读属性封装到单个对象中,而无需首先显式定义一个类型。

   2.类型名由编译器生成,并且不能在源代码级使用。每个属性的类型由编译器推断。

   3.可通过使用 new 运算符和对象初始值创建匿名类型。

可通过将隐式键入的本地变量与隐式键入的数组相结合创建匿名键入的元素的数组,如下面的示例所示。

 var anonArray = new[] { new { name = "apple", diam = 4 }, new { name = "grape", diam = 1 }};

扩展方法

View Code

 public class Student
    {
        public int Age { get; set; }
    }
    public static class ExtendMethods
    {
        public static string Hello(this Student stu)
        {
            return "hello extend method" + stu.Age;
        }
    }

 static void Main()
        {
            Student stu = new Student { Age = 10 };
            Console.WriteLine(stu.Hello());
        }

如上例所示,即为Student类增加了一个Hello的扩展方法,效果等同于在Student类中增加了一个静态方法,但该方法只能访问Student类中的公共变量,常用于给第三方库扩展方法。