C# 6.0新特性
作者:互联网
C# 6.0新特性
01.自动属性新初始化方法
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 namespace NewDemo 7 { 8 //以前用法:自动属性不能在声明的时候初始化 9 //class Student 10 //{ 11 // public Student() 12 // { 13 // StudentId = 1001; 14 // Name = "张欣欣"; 15 // Age = 25; 16 // } 17 // public int StudentId { get; set; } 18 // public string Name { get; set; } 19 // public int Age { get; set; } 20 // public string Gender 21 // { 22 // get { return Gender; } 23 // } 24 //} 25 //新用法:声明的同时可以初始化,并且允许只读属性初始化 26 class Student 27 { 28 public int StudentId { get; set; } = 1001; 29 public string Name { get; set; } = "张欣欣"; 30 public int Age { get; set; } = 25; 31 public string Gender { get; } = "男"; 32 } 33 }View Code
02.String.Format的简化
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 namespace NewDemo 7 { 8 class NewFormat 9 { 10 //旧用法:string.Format("{0},{1}",变量1,变量2)实现格式化输出 11 public void OldMethod() 12 { 13 Student objStudent = new NewDemo.Student(); 14 string s1 = string.Format("{0},{1}", objStudent.Name, objStudent.Age); 15 string s2 = string.Format("姓名={0},年龄={1}", objStudent.Name, objStudent.Age); 16 Console.WriteLine("{0},\r\n{1}", s1, s2); 17 Console.WriteLine(); 18 string s3 = string.Format("{0,10},{1:d3}", objStudent.Name, objStudent.Age); 19 string s4 = string.Format("{0,10},{1,10:d3}", objStudent.Name, objStudent.Age); 20 Console.WriteLine("{0},\r\n{1}", s3, s4); 21 Console.WriteLine(); 22 string s5 = string.Format("{0:yyyy-MM-dd}", DateTime.Now); 23 Console.WriteLine(s5); 24 Console.WriteLine(); 25 string sql = "select Name from Students where StudentId={0} and Age>{1}"; 26 Console.WriteLine(sql, objStudent.StudentId, objStudent.Age); 27 } 28 //新用法:在字符串前面添加“$”前缀,(变量可以直接写到{}内,并且有很强的智能提示) 29 public void NewMethod() 30 { 31 Student objStudent = new NewDemo.Student(); 32 string s1 = $"{objStudent.Name },{objStudent.Age }"; 33 string s2 = $"姓名={objStudent.Name },年龄={objStudent.Age }"; 34 Console.WriteLine($"{ s1} ,\r\n{ s2} "); 35 string s3 = $"{objStudent.Name,10},{objStudent.Age:d3}"; 36 string s4 = $"{objStudent.Name,10},{objStudent.Age,10:d3}"; 37 Console.WriteLine($"{ s3} ,\r\n{ s4} "); 38 string s5 = $"{DateTime.Now:yyyy-MM-dd}"; 39 Console.WriteLine(s5); 40 //典型应用 41 string sql = $"select Name from Students where StudentId={objStudent.StudentId} and Age>{objStudent.Age }"; 42 Console.WriteLine( sql); 43 } 44 } 45 }View Code
03.表达式属性和表达式方法
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 namespace NewDemo 7 { 8 /// <summary> 9 /// 表达式应用的新特性 10 /// </summary> 11 class ExpressionApp 12 { 13 //【1】表达式属性:只有一个get访问器的单行属性可以使用lambda语法编写 14 public DateTime Birthday { get; set; } = Convert.ToDateTime("1990-1-10"); 15 //public int Age 16 //{ 17 // get { return DateTime.Now.Year - Birthday.Year; } 18 //} 19 public int Age => DateTime.Now.Year - Birthday.Year; 20 //【2】表达式方法:只有一条语句的方法可以使用lambda语法编写 21 //public int Add(int a, int b) 22 //{ 23 // return a + b; 24 //} 25 public int Add(int a, int b) => a + b; 26 } 27 }View Code
04.泛型集合Dictionary初始化
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 namespace NewDemo 7 { 8 /// <summary> 9 /// 泛型集合的新初始化方法 10 /// </summary> 11 class NewCollectionInit 12 { 13 public Dictionary<string, int> OldMethod() 14 { 15 Dictionary<string, int> student = new Dictionary<string, int>(); 16 student.Add("张三", 25); 17 student.Add("李四", 34); 18 student.Add("王五", 26); 19 return student; 20 } 21 //新的初始化方法 22 public Dictionary<string, int> NewMethod() 23 { 24 Dictionary<string, int> student = new Dictionary<string, int>() 25 { 26 ["张三"] = 25, 27 ["李四"] = 34, 28 ["王五"] = 26 29 }; 30 return student; 31 } 32 } 33 }View Code
05.static声明静态类的引用
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using static System.Math; 7 8 namespace NewDemo 9 { 10 /// <summary> 11 /// static声明静态类的引用 12 /// </summary> 13 class StaticClassApp 14 { 15 //以前用法:两个数的绝对值相加 16 public static int OldMethod(int a, int b) 17 { 18 return Math.Abs(a) + Math.Abs(b); 19 } 20 //现在用法:使用using static System.Math;提前引入静态类,避免每次都调用Math类 21 public static int NewMethod1(int a, int b) 22 { 23 return Abs(a) + Abs(b); 24 } 25 public static int NewMethod2(int a, int b) => Abs(a) + Abs(b); 26 } 27 }View Code
06.nameof表达式
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 namespace NewDemo 7 { 8 /// <summary> 9 /// nameof表达式 10 /// </summary> 11 class NameofExpressions 12 { 13 //以前用法:当参数名称变化的时候,被引用地方需要同步修改 14 public void OldMethod(int account) 15 { 16 if (account < 100) 17 { 18 throw new ArgumentException("参数account的值不能小于100!"); 19 } 20 else 21 { 22 //其他操作... 23 } 24 } 25 //新用法:使用nameof,当参数变化时会在引用的地方同步变化,避免程序的硬编码 26 //nameof里面可以是:类名、方法名、参数名、属性名 27 public void NewMethod(int account) 28 { 29 if (account < 100) 30 { 31 throw new ArgumentException($"参数{nameof(account)}的值不能小于100!"); 32 } 33 else 34 { 35 //其他操作... 36 } 37 } 38 } 39 }View Code
07.Null条件表达式
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 namespace NewDemo 7 { 8 /// <summary> 9 /// null操作符:null传递操作符简化了空值的检查 10 /// </summary> 11 class NullOperator 12 { 13 string[] sArray = new string[] { "bc", "cde", null, "efgg", null }; 14 //以前用法 15 public void OldMethod() 16 { 17 foreach (string item in sArray) 18 { 19 var length = item == null ? 0 : item.Length; 20 Console.WriteLine(length); 21 } 22 Console.WriteLine("---"); 23 } 24 //新方法: 25 public void NewMethod() 26 { 27 foreach (string item in sArray) 28 { 29 var length = item?.Length;//如果为null直接输出null 30 Console.WriteLine(length); 31 } 32 Console.WriteLine("---"); 33 foreach (string item in sArray) 34 { 35 var length = item?.Length ?? 0; 36 Console.WriteLine(length); 37 } 38 } 39 } 40 }View Code
END
标签:string,C#,System,特性,int,6.0,using,objStudent,public 来源: https://www.cnblogs.com/zeon/p/16324839.html