VS2015預覽版中的C#6.0 新功能(三)express
VS2015的預覽版在11月12日發佈了,下面讓咱們來看看C#都提供了哪些新的功能。安全
字符串添寫(String interpolation)
在格式化字符串時,string.Format是常常被用到的,它確實很方便使用,可是這種使用佔位符,而後經過參數替換的方式還不夠方便, 在C#6.0裏,String interpolation語法的引入提供了另外一種格式化字符串的方式。請看下面的例子:
假設咱們如今有個以下所示的Book類,如今須要格式化它的字段以輸出關於該book的描述。 post
public class Book { public int Number { get; set; } public string Name { get; set; } public string Abstract { get; set; } public float Price { get; set; } public List<Author> Authors { get; set; } }
使用string.Format的代碼以下:url
var introUsingStringFormat = string.Format("[{0}]' price is {1:F3}, its description is {2}.", book.Name, book.Price, book.Abstract);
使用string interpolation的代碼以下:spa
var introUsingStrInterPolation = "[\{book.Name}]' price is \{book.Price : F3}, its description is \{book.Abstract}.";
完整的程序以下:線程
public void Show() { //interpolate string var book = new Book { Abstract = "Book about C#6.0", Name = "C#6.0 new feature", Price = 10.8709f, }; var introUsingStrInterPolation = "[\{book.Name}]' price is \{book.Price : F3}, its description is \{book.Abstract}."; var introUsingStringFormat = string.Format("[{0}]' price is {1:F3}, its description is {2}.", book.Name, book.Price, book.Abstract); Console.WriteLine("format string using string interpolation:"); Console.WriteLine(introUsingStrInterPolation); Console.WriteLine("==============================================================================="); Console.WriteLine("format string using string.Format method:"); Console.WriteLine(introUsingStringFormat); Console.Read(); }
以下圖,兩種方式的輸出是同樣的:code
總結:orm
String Interpolation語法容許你在字符串裏直接插入代碼並能夠像string.Format 那樣指定format Specifier和對齊,如上面的例子\{book.Price : F3}指定price的精度爲3。這個語法在以後版本中會變得更加簡潔,可能會採用以下的格式:
htm
var introUsingStrInterPolation = $"[{book.Name}]' price is {book.Price : F3}, its description is {book.Abstract}.";
空條件運算符?
以下面例子所示, 在程序中常常會出現對錶達式中對象是否爲空的連續檢測。
if (book != null && book.Authors != null) { var countOfAuthers = book.Authors.Count; }
空條件運算符?使得這種檢測更加方便,表達更加簡潔,其使用方法以下:
var countOfAuthersUsingNullConditional = book?.Authors?.Count;
空條件運算符?用在成員運算符.和索引前面,會執行下面的操做:
若是其前面的對象爲空,那麼直接返回null,不然容許訪問前面對象的成員或者元素以繼續後面運算,因此上面的表達式和下面的代碼段是等價的
if (book == null) { countOfAuthorsUsingNullConditional = null; } else if (book.Authors == null) { countOfAuthorsUsingNullConditional = null; } else { countOfAuthorsUsingNullConditional = book.Authors.Count; }
上面的code展現了其執行的邏輯順序,達到相同結果的簡潔寫法以下:
if(book == null || book.Authors == null) { countOfAuthorsUsingNullConditional = null; } else { countOfAuthorsUsingNullConditional = book.Authors.Count; }
從上能夠看出其具備以下特性:
此外,空條件運算符還具備以下特色:
下面來看一些針對2和3的例子:
//using with coalescing operator ?? int numberOfAuthors = book?.Authors?.Count ?? 0; //using with delegate. action?.Invoke();
完整的程序以下:
public void Show() { //traditional way if (book != null && book.Authors != null) { var countOfAuthors = book.Authors.Count; Console.WriteLine("===================using tranditional way=============="); Console.WriteLine(countOfAuthors); } //the way of using null-conditional operator. var countOfAuthorsUsingNullConditional = book?.Authors?.Count; Console.WriteLine("===================null-conditional operator=============="); Console.WriteLine(countOfAuthorsUsingNullConditional); Console.Read(); //the logic of the expression. if (book == null) { countOfAuthorsUsingNullConditional = null; } else if (book.Authors == null) { countOfAuthorsUsingNullConditional = null; } else { countOfAuthorsUsingNullConditional = book.Authors.Count; } //the concise edition using tranditional way. if (book == null || book.Authors == null) { countOfAuthorsUsingNullConditional = null; } else { countOfAuthorsUsingNullConditional = book.Authors.Count; } //using with coalescing operator ?? int numberOfAuthors = book?.Authors?.Count ?? 0; //using with delegate. action?.Invoke(); }
nameof表達式
有時候咱們須要得到代碼中某些symbol的名字,例如在throw ArgumentNullException時,須要得到爲null參數的名字(字符串形式),在調用PropertyChanged時,咱們也須要得到屬性的名字,直接使用字符串具備以下的缺點:
nameof表達式可以以字符串的形式返回參數對象或者類成員的名字,下面是一些例子
var nameOfClassPropertyObject = nameof(book); var nameOfArgument = nameof(author); var classMethodMember = nameof(Book.Equals); var classPropertyMember = nameof(Book.Number); var @class = nameof(Book);
從上面的例子中能夠看出nameof運算符能夠用於類(包括attribute類),類的成員,對象上,另外須要注意的是它只會輸出最終元素的名字不會包含其前綴,例如nameof(Book.Equals)的輸出是Equals。