字符串格式化,自动化属性赋值
/字符串格式化,自动化属性赋值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//string[] names = { "ganquanfu", "ligang", "zhongshi" };
//var res = names.Where(name => (name.IndexOf("gan") > -1));
//foreach (var item in res)
//{
// Console.WriteLine(item);
//}
string format = string.Empty;
List<People> peoples = new List<People> (){
new People() { Name = "huhong", Age = 23 },
new People() { Name = "ligang", Age = 42 } ,
new People() { Name = "xiongbing", Age = 23 },
new People() { Name = "xiaoming", Age = 42 }
};
Console.WriteLine(string.Format("{0,-10}\t\t{1}", "姓名","年龄"));
foreach (var item in peoples)
{
format = string.Format("{0,-10}\t\t{1}", item.Name, item.Age);//负数表示左对齐,整数表示右对齐
Console.WriteLine(format);
}
Console.Read();
}
}
class People
{
public string Name { get; set; }
public int Age { get; set; }
}
}