people类
public class People { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } /// <summary> ///C#6新语法. :8.自动实现的属性初始化器 /// </summary> public DateTime BirthDay { get; set; } = DateTime.Now.AddYears(-22); /// <summary> /// C#6新语法:在属性/方法里使用Lambda表达式(Expression bodies on property-like function members) /// </summary> public string NameFormat => string.Format("姓名:{0}", "NameFormat"); /// <summary> /// C#6新语法:在属性/方法里使用Lambda表达式(Expression bodies on property-like function members) /// </summary> public void Print() => WriteLine(NameFormat); }
1.导入静态类:
之前的写法:静态类.
{ Console.WriteLine("Hello!");
Console.WriteLine($"之前的使用方式[静态类.]:{Math.Max(2, 3)}");
}
C#6的写法:
//引入命名空间 using static System.Math; using static System.Console;
WriteLine("Hello!"); WriteLine($"现在的使用方式引入命名空间,这边直接调用:{ Max(2, 3)}");
2.字符串嵌入值
#region 2.字符串嵌入值(String interpolation) { WriteLine($"Id:{people.Id} 姓名:{people.Name} 生日:{people.BirthDay.ToString("yyyy-MM-dd")}"); WriteLine($"{(people.Age >= 25 ? "腊肉" : "鲜肉")}"); WriteLine($"Id:{{{people.Id}}} 姓名:{{{people.Name}}}"); }
没有评论:
发表评论