Blogger Information
Blog 2
fans 0
comment 0
visits 264
Related recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
C# 中的 ?? 运算符
沈星繁
Original
264 people have browsed it

?? 运算符

当前面的表达式为 null 时,返回后面表达式的值。

例如:

  1. public int NotnullOrDefault(int? nullableInt, int defaultInt)
  2. {
  3. return nullableInt ?? defaultInt;
  4. }

后面的表达式也可以是 throw 语句,通常抛出 ArgumentNullException

例如:

  1. var code = "9536e2f5-6beb-4ff2-85e2-62871cf09cfa";
  2. Console.WriteLine(EnsureNotNull(code));
  3. string EnsureNotNull(string? nullableString)
  4. {
  5. return nullableString ?? throw new ArgumentNullException(nameof(nullableString));
  6. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post