您的位置:首页 > 移动开发

Visual C# 2010 Recipes A Problem-Solution Approach 代码3—— 创建匿名类型

2010-06-14 22:36 295 查看
using System;

namespace Apress.VisualCSharpRecipes.Chapter01
{

public class Recipe01_18
{
static void Main(string[] args)
{

// create an anoymous type
var joe = new {
Name = "Joe Smith",
Age  = 42,
Family = new {
Father = "Pa Smith",
Mother = "Ma Smith",
Brother = "Pete Smith"
},
};

// access the members of the anonymous type
Console.WriteLine("Name: {0}", joe.Name);
Console.WriteLine("Age: {0}", joe.Age);
Console.WriteLine("Father: {0}", joe.Family.Father);
Console.WriteLine("Mother: {0}", joe.Family.Mother);
Console.WriteLine("Brother: {0}", joe.Family.Brother);

Console.WriteLine("Main method complete. Press Enter.");
Console.ReadLine();
}
}
}


一个使用var关键字创建匿名类型的实例,例子举得不错,可以做个参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐