鉴于以下示例,我将如何clientList
在第二个示例中包含 5 个客户端?
我希望该list.Contains()
方法仅检查FName
andLName
字符串并在检查相等性时忽略年龄。
struct client
{
public string FName{get;set;}
public string LName{get;set;}
public int age{get;set;}
}
示例 1:
List<client> clientList = new List<client>();
for (int i = 0; i < 5; i++)
{
client c = new client();
c.FName = "John";
c.LName = "Smith";
c.age = 10;
if (!clientList.Contains(c))
{
clientList.Add(c);
}
}
//clientList.Count(); = 1
示例 2:
List<client> clientList = new List<client>();
for (int i = 0; i < 5; i++)
{
client c = new client();
c.FName = "John";
c.LName = "Smith";
c.age = i;
if (!clientList.Contains(c))
{
clientList.Add(c);
}
}
//clientList.Count(); = 5