• C#3.0新特性之匿名类型


     

    示例代码如下,控制台应用程序:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace AnonymousTypeDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                var personWang = new
                {
                    Name = "张三",
                    Sex = true,
                    Age = 27
                };

                Console.WriteLine("Name属性:{0}",personWang.Name);
                Console.WriteLine("Sex属性:{0}", personWang.Sex);
                Console.WriteLine("Age属性:{0}", personWang.Age);

                var personWang1 = new
                {
                    Name = "张三",
                    Sex = true,
                    Age = 34            };

                var personWang2 = new
                {
                    Sex = true,
                    Name = "张三",
                    Age = 27
                };

                if (personWang.GetType() == personWang1.GetType())
                {
                    Console.WriteLine(@"personWang\personWang1两个对象是相同类型");
                }
                else
                {
                    Console.WriteLine(@"personWang\personWang1两个对象是不同类型");
                }

                if (personWang1.GetType() == personWang2.GetType())
                {
                    Console.WriteLine(@"personWang1\personWang2两个对象是相同类型");
                }
                else
                {
                    Console.WriteLine(@"personWang1\personWang2两个对象是不同类型");
                }

                if (personWang == personWang1)
                {
                    Console.WriteLine("两个对象是同一个对象");
                }
                else
                {
                    Console.WriteLine("两个对象不是同一个对象");
                }
            }
        }
    }

    尊重作者,转发请注明出处:http://www.cnblogs.com/minotmin
    谢谢阅读,有错请指出,不甚感激。

  • 相关阅读:
    【转载】我的七个建议Joel Spolsky
    C语言文件读写操作
    【转】RO段、RW段和ZI段 Image$$??$$Limit 含义(zz)
    给大家一个测试webservice的软件
    .net 实现深拷贝的方法
    第一次设计数据访问层,大家给你建议,谢谢
    重新写博客
    (转)理解 Thread.Sleep 函数
    slk解压缩
    SMTP Service设置
  • 原文地址:https://www.cnblogs.com/minotmin/p/2701849.html
Copyright © 2020-2023  润新知