iT邦幫忙

3

類型的預設值

c#
WM 2020-04-13 23:50:291094 瀏覽
  • 分享至 

  • xImage
  •  

C# 7.1 之前的寫法:

int i = default(int);
string s = default(string);
Console.WriteLine(i);
Console.WriteLine(s ?? "null");

C# 7.1 之後的寫法:

int i = default;
string s = default;
Console.WriteLine(i);
Console.WriteLine(s ?? "null");

??運算子的意思是:若s不為null,就輸出內容;若為null,就輸出後面的"null"字串。
https://ithelp.ithome.com.tw/upload/images/20200413/20112573Eu1hT9f01W.png
參考資料:
https://docs.microsoft.com/zh-tw/dotnet/csharp/language-reference/builtin-types/default-values


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言