System.Text.StringBuilder sb =null;// No error; s instead evaluates to nullstring s = sb?.ToString();//等于 string s = (sb == null ? null : sb.ToString());
string foo =null;char? c = foo?[1];// c is null, char? 表示让char可以为null的意思//具体见 Nullale Value Types
System.Text.StringBuilder sb =null;// s evaluates to null without errorstring s = sb?.ToString().ToUpper();
//If someObject is null, this becomes a “no-operation” //rather than throwing a NullReferenceException.
someObject?.SomeVoidMethod();
//可以混合使用System.Text.StringBuilder sb =null;string s = sb?.ToString()??"nothing";