C# 怎么将匿名对象添加到列表(List)

2020年2月14日21:45:09 评论 583字阅读1分56秒

1.简介

C# 怎么将匿名对象添加到列表(List)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
using static System.Console;
using System;
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      var p2 = new Person
      {
         Name = "Back",
         DateOfBirth = new DateTime(1998, 3, 17)
      };
      p2.Children.Add(new Person { Name = "A" });
      p2.Children.Add(new Person { Name = "Z" });
      WriteLine($"{p2.Name} has {p2.Children.Count} children:");
      for (int child = 0; child < p2.Children.Count; child++)
      {
         WriteLine($"  {p2.Children[child].Name}");
      }
   }
}
public class Person : object
{
   
   public string Name;
   public DateTime DateOfBirth;
   public List<Person> Children = new List<Person>();
}

 
编程十万个怎么办
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: