110,502
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
List<Person> personList = new List<Person>();
personList.AddRange(new Person[]
{
new Person(){ name="张三", age=22, telephone="13957902234"},
new Person(){ name="李四", age=21, telephone="12345678901"},
});
Person newPerson = new Person()
{
name = "张三",
age = 11,
telephone = "13957902234221",
};
//确认新添加的元素的name与personList所有元素的name不同
bool result = personList.All(x => !x.name.Equals(newPerson.name));
if (result)
{
personList.Add(newPerson);
}
//person2的name与person1的name相同则不添加。所以personList的Count为2
Console.WriteLine(personList.Count);
}