C#3.0新特性之匿名类型

本文提供了一个使用C#匿名类型的示例程序,演示了如何创建匿名类型对象并比较它们的类型。通过示例展示了即使属性顺序不同,只要属性名称和类型相同,则会被认为是相同的类型。

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

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/aaaaa
谢谢阅读,有错请指出,不甚感激。

转载于:https://www.cnblogs.com/aaaaa/archive/2012/09/25/2746865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值