如何将列表中的某些变量写入控制台 C#
how to write certain variables in a list to the console C#
Stylist stylist1 = new Stylist(); // creates new stylist
stylist1.FirstName = "john";
stylist1.LastName = "smith";
stylist1.Phone = "123456789";
stylist1.Email = "js@123.456";
stylist1.Rate = "123";
Stylist stylist2 = new Stylist(); // creates new stylist
stylist2.FirstName = "jane";
stylist2.LastName = "doe";
stylist2.Phone = "987654321";
stylist2.Email = "jd@987.654";
stylist2.Rate = "456";
List<Stylist> stylists = new List<Stylist>(); // creates stylist
list
stylists.Add(stylist1);
stylists.Add(stylist2);
我想将列表的内容显示到控制台,但我只想显示 FirstName
和 LastName
,我该怎么做?我还做了一个名为 Stylist
的客户 class
public class Stylist
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Rate { get; set; }
}
编辑
如果我把每一个都从列表中单独写出来,我就能写出我需要的东西
Console.WriteLine(stylist1.FirstName + " " + stylist1.LastName);
我试过使用 foreach
foreach (object i in stylists)
{
Console.WriteLine(i);
}
但这只会打印“ACW_Programming2.Stylist
ACW_Programming2是项目名
foreach (var i in stylists)
{
Console.WriteLine(i.FirstName + " " + i.LastName);
}
作为 Giorgi 答案的替代方法,您可以覆盖 Stylist 的 ToString 方法
public class Stylist
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Rate { get; set; }
public override string ToString()
{
return FirstName + " " + LastName;
}
}
然后做你做的事
foreach (Stylist i in stylists)
{
Console.WriteLine(i);
}
Stylist stylist1 = new Stylist(); // creates new stylist
stylist1.FirstName = "john";
stylist1.LastName = "smith";
stylist1.Phone = "123456789";
stylist1.Email = "js@123.456";
stylist1.Rate = "123";
Stylist stylist2 = new Stylist(); // creates new stylist
stylist2.FirstName = "jane";
stylist2.LastName = "doe";
stylist2.Phone = "987654321";
stylist2.Email = "jd@987.654";
stylist2.Rate = "456";
List<Stylist> stylists = new List<Stylist>(); // creates stylist
list
stylists.Add(stylist1);
stylists.Add(stylist2);
我想将列表的内容显示到控制台,但我只想显示 FirstName
和 LastName
,我该怎么做?我还做了一个名为 Stylist
public class Stylist
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Rate { get; set; }
}
编辑
如果我把每一个都从列表中单独写出来,我就能写出我需要的东西
Console.WriteLine(stylist1.FirstName + " " + stylist1.LastName);
我试过使用 foreach
foreach (object i in stylists)
{
Console.WriteLine(i);
}
但这只会打印“ACW_Programming2.Stylist
ACW_Programming2是项目名
foreach (var i in stylists)
{
Console.WriteLine(i.FirstName + " " + i.LastName);
}
作为 Giorgi 答案的替代方法,您可以覆盖 Stylist 的 ToString 方法
public class Stylist
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Rate { get; set; }
public override string ToString()
{
return FirstName + " " + LastName;
}
}
然后做你做的事
foreach (Stylist i in stylists)
{
Console.WriteLine(i);
}