C# 7.0 语法

 C# 7.0的语法主要是优化了之前的写法,使得更加简洁方便。try catch when  这个使用场景很少,正常的开发无业务处理的时候不建议使用 。

#region 2.字符串嵌入值
Console.WriteLine("____________字符串嵌入值____________");
Console.WriteLine(string.Format("当前时间:{0}", DateTime.Now.ToString()));

Console.WriteLine($"6当前时间:{ DateTime.Now.ToString()}");
#endregion

#region 3.空值运算符
Console.WriteLine("____________空值运算符____________");
string name = null;
Console.WriteLine(string.IsNullOrEmpty(name) ? "" : name.ToString());//避免空异常

string str = string.Empty;
Console.WriteLine(name?.ToString());
#endregion

#region 4.异常过滤器
Console.WriteLine("____________异常过滤器____________");
int exceptionValue = 10;
try
{
Int32.Parse("s");
}
catch (Exception e)
when (exceptionValue > 1)//满足条件才进入catch
{
Console.WriteLine("catch");
}
//QuartzManager.addJob("datetimeUpdate", "test", "ConsoleApp.DemoJob", "0/20 * * * * ?");
#endregion
Console.ReadLine();

猜你喜欢

转载自www.cnblogs.com/bingli/p/11137706.html