焊马 發表於 2022-4-23 23:20:00

C#中检查null的语法糖

<p>今天看到已经更新了devblogs,新增的C# 11的!!(用于检查null的语法)经过非常长的讨论,最后取消了。然后我又想起来null检查,这个可以说一说。</p>
<h2 id="函数参数null检查">函数参数null检查</h2>
<h3 id="传统写法">传统写法</h3>
<p>写一个函数的时候,最经典的检查,估计也是大家最常使用的null检查,应该是这样的吧:</p>
<pre><code class="language-c#">public static void GetV1(string prompt)
{
    if (prompt is null) throw new ArgumentNullException(nameof(prompt));
    Console.WriteLine(prompt);
}
</code></pre>
<h3 id="throwifnull">ThrowIfNull</h3>
<p>这个写起来也没啥问题,但是总觉得有点不舒适。.NET 6在<code>ArgumentNullException</code>中新增了<code>ThrowIfNull</code>方法,可以写的更优雅一点。</p>
<pre><code class="language-c#">public static void GetV2(string prompt)
{
    ArgumentNullException.ThrowIfNull(prompt);
    Console.WriteLine(prompt);
}
</code></pre>
<p>异常的时候,就会出现:<code>System.ArgumentNullException: 'Value cannot be null. (Parameter 'prompt')'</code>。这个是不是简单了点?可是还是需要写一行。</p>
<h3 id="c-11的语法已经取消">C# 11的!!语法(已经取消)</h3>
<p>C# 11刚preview的时候,我就瞄到了这个特性,现在依然可以通过设置<langversion>preview</langversion>来进行启用,但是以后正式发布估计就不行了。</p>
<p>它通过在参数后面叠加!!表示此参数不可为空,运行时会自动进行检查,如果是null就直接弹出错误。</p>
<pre><code class="language-c#">public static void GetV3(string prompt!!)
{
    Console.WriteLine(prompt);
}
</code></pre>
<p>这个代码会被编译器翻译成:</p>
<pre><code class="language-C#">public static void GetV3(string prompt!!)
{
    if (prompt is null) {
      throw new ArgumentNullException(nameof(prompt));
    }
    Console.WriteLine(prompt);
}
</code></pre>
<p>这样大家就可以专注于业务代码,不需要经常考虑异常检查了。至于为什么这个东西最后还是被删除了,可以从讨论中看到一丝端倪,首先是感觉非常纠结于这个语法,两个叹号;然后就是已经有比较多的方式可以实现检查了,这个东西是否有必要。反正最终是以后再讨论了,不过也可以看出来C#语言工作组对语言的特性讨论非常谨慎。</p>
<p>他们还讨论了很多别的形式,每种都提出了各自的优缺点挺有意思的,能看出来有一点设计语言的严谨和小强迫症在,点赞~</p>
<pre><code>void M(string s!!);
void M(string! s);
void M(string s!);
void M(notnull string s);
void M(string s ?? throw);
void M(string s is not null);
void M(checked string s);
void M(string s) where s is not null;
</code></pre>
<h2 id="有关null的一些操作">有关null的一些操作</h2>
<p>说起这个,就顺便说说c#处理null的另外几个语法糖吧。</p>
<h3 id="_">??</h3>
<p>如果左边是的null,那么返回右边的操作数,否则就返回左边的操作数,这个在给变量赋予默认值非常好用。</p>
<pre><code class="language-c#">int? a = null;
int b = a ?? -1;
Console.WriteLine(b);// output: -1
</code></pre>
<h3 id="_-1">??=</h3>
<p>当左边是null,那么就对左边的变量赋值成右边的</p>
<pre><code class="language-C#">int? a = null;
a ??= -1;
Console.WriteLine(a);// output: -1
</code></pre>
<h3 id="_-2">?.</h3>
<p>当左边是null,那么不执行后面的操作,直接返回空,否则就返回实际操作的值。</p>
<pre><code class="language-c#">using System;
public class C {
    public static void Main() {
      string i = null;
      int? length = i?.Length;
      Console.WriteLine(length ?? -1); //output: -1
    }
}
</code></pre>
<h3 id="_-3">?[]</h3>
<p>索引器操作,和上面的操作类似</p>
<pre><code class="language-c#">using System;
public class C {
    public static void Main() {
      string[] i = null;
      string result = i?;
      Console.WriteLine(result ?? "null"); // output:null
    }
}
</code></pre>
<blockquote>
<p>注意,如果链式使用的过程中,只要前面运算中有一个是null,那么将直接返回null结果,不会继续计算。下面两个操作会有不同的结果。</p>
<pre><code class="language-c#">using System;
public class C {
    public static void Main() {
      string[] i = null;
      Console.WriteLine(i??.Substring(0).Length); //不弹错误
      Console.WriteLine((i??.Substring(0)).Length) // System.NullReferenceException: Object reference not set to an instance of an object.
    }
}
</code></pre>
</blockquote>
<h2 id="一些操作">一些操作</h2>
<pre><code class="language-c#">//参数给予默认值
if(x == null) x = "str";
//替换
x ??= "str";


//条件判断
string x;
if(i&lt;3)
    x = y;
else
{
    if(z != null) x = z;
    else z = "notnull";
}
//替换
var x = i &lt; 3 ? y : z ?? "notnull"


//防止对象为null的时候,依然执行代码
if(obj != null)
    obj.Act();
//替换
obj?.Act();

//Dictionary取值与赋值
string result;
if(dict.ContainKey(key))
{
    if(dict == null) result = "有结果为null";
    else result = dict;
}
else
    result = "无结果为null";
//替换
var result= dict.TryGetValue(key, out var value) ? value ?? "有结果为null" : "无结果为null";
</code></pre>
<h2 id="结语">结语</h2>
<p>原来新定的C# 11提供了一个新的??,话说我个人还是挺喜欢这个特性的,不管以什么形式出现吧,期待以后再见。</p>
<blockquote>
<p>C#中为了处理null给我们准备了许多的语法糖,只能说非常简便了。有很多人会说这个可读性不好,或者觉得这是“茴字的几种写法”似的歪门邪道,我只能说,传统的语法也不是说取消了,语言有发展,只要还是比较审慎的,我觉得还是一件好事。</p>
</blockquote>
<h2 id="参考资料">参考资料</h2>
<ul>
<li>https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-</li>
<li>https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/null-coalescing-operator</li>
</ul>
<h2 id="后记">后记</h2>
<p>一定要夸一下博客园,写完这篇文章想登陆博客园发布的时候,被登陆卡住了。弹出来和google一样的验证画面,找红绿灯找人行横道什么的,只能说我找了几分钟也没找明白,我确信我已经点的正确了,所有的区块,占上了有一点算不算?不知道也没有反馈,就点呀点呀,密码我已经按照要求设置的够复杂了,还有必要通过这种反人类的东西来验证吗?不理解,京东阿里之类的购物网站的验证也就拖一下完事,这博客园的后台估计比那些个地方还要敏感吧,赞一个!太赞了!</p><br><br>
来源:https://www.cnblogs.com/podolski/p/16184027.html
頁: [1]
查看完整版本: C#中检查null的语法糖