C# ??符号

 

复制代码
 1  static void Main(string[] args)
 2         {
 3             string strA = String.Empty;
 4             string strB = String.Empty;
 5             strA = null;
 6             strB = "strB";
 7             /**
 8              * 如果??左边的strA值为String.IsNullOrWhiteSpace则StrC=strB
 9              * 如果??右边的strB值为String.IsNullOrWhiteSpace则StrC=strA
10              */
11             string StrC = strA ?? strB;
12             Console.WriteLine(StrC);
13 
14             strB = null;
15             strA = "strA";
16             Console.WriteLine(strA ?? strB);
17 
18             strB = String.Empty;
19             strA = "No Empty";
20             string a = strA ?? strB;
21             Console.WriteLine(a == null ? "null" : a);
22             Console.ReadLine();
23         }
复制代码

输出

posted @   J.Y  阅读(956)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示