class NewFormat
{
//旧用法:string.Format("{0},{1}",变量1,变量2)实现格式化输出
publicvoidOldMethod()
{
Student objStudent=newNewDemo.Student();
strings1=string.Format("{0},{1}", objStudent.Name, objStudent.Age);
strings2=string.Format("姓名={0},年龄={1}", objStudent.Name, objStudent.Age);
Console.WriteLine("{0},\r\n{1}", s1, s2);
Console.WriteLine();
strings3=string.Format("{0,10},{1:d3}", objStudent.Name, objStudent.Age);
strings4=string.Format("{0,10},{1,10:d3}", objStudent.Name, objStudent.Age);
Console.WriteLine("{0},\r\n{1}", s3, s4);
Console.WriteLine();
strings5=string.Format("{0:yyyy-MM-dd}", DateTime.Now);
Console.WriteLine(s5);
Console.WriteLine();
stringsql="select Name from Students where StudentId={0} and Age>{1}";
Console.WriteLine(sql, objStudent.StudentId, objStudent.Age);
}
//新用法:在字符串前面添加“$”前缀,(变量可以直接写到{}内,并且有很强的智能提示)
public void NewMethod()
{
Student objStudent=newNewDemo.Student();
strings1=$"{objStudent.Name },{objStudent.Age }";
strings2=$"姓名={objStudent.Name },年龄={objStudent.Age }";
Console.WriteLine($"{ s1} ,\r\n{ s2} ");
strings3=$"{objStudent.Name,10},{objStudent.Age:d3}";
strings4=$"{objStudent.Name,10},{objStudent.Age,10:d3}";
Console.WriteLine($"{ s3} ,\r\n{ s4} ");
strings5=$"{DateTime.Now:yyyy-MM-dd}";
Console.WriteLine(s5);
//典型应用
stringsql=$"select Name from Students where StudentId={objStudent.StudentId} and Age>{objStudent.Age }";
Console.WriteLine( sql);
}
}
留言