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关键字创建匿名类型的实例,例子举得不错,可以做个参考
相关文章推荐
- Visual C# 2010 Recipes A Problem-Solution Approach 代码4——创建ExpandoObject Dynamic Type
- Visual C# 2010 Recipes A Problem-Solution Approach 代码4——自动完成属性
- Visual C# 2010 Recipes A Problem-Solution Approach 代码2 —— Create a Windows-Based Application from the Command Line
- Visual C# 2010 Recipes A Problem-Solution Approach 代码6——运算符重载
- Visual C# 2010 Recipes A Problem-Solution Approach 代码一 —— Create a Console Application from the Command Line
- Visual C# 2010 Recipes A Problem-Solution Approach 代码7——定义转换运算符
- Visual C# 2010 Recipes: A Problem-Solution Approach
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- 在Visual C# 2.0中创建优雅代码3
- msdn创建自定义 SharePoint 2010 字段类型
- [设计模式]在CodeDom代码生成中使用Decorator模式实现类型创建
- [转]C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- 在SharePoint 2010中创建自定义字段类型
- sharepoint 2010 如何创建文档库内容类型content type
- VS 2010错误代码C2143: 语法错误 : 缺少“;”(在“类型”的前面) 解决方法
- C# 2.0:使用匿名方法、迭代程序和局部类来创建优雅的代码
- 在PowerShell中调用C#代码创建对象类型
- SP2010开发和VS2010专家"食谱"--第四章节—列表定义和内容类型(1)--创建网站栏
- teamfoundationserver2010之使用vs2010&&vs2013创建修改签入签出提交代码等操作
- 在Visual C# 2.0中创建优雅代码4