ReSharper 2026.1 Help

代码检查:使用 'ArgumentException.ThrowIfNullOrEmpty/ThrowIfNullOrWhiteSpace'

.NET 8.0 起, ArgumentException 类提供了静态方法 ThrowIfNullOrEmpty()ThrowIfNullOrWhiteSpace() 用于简化参数校验。

该检查会识别手动进行的空值或空字符串/空白字符检查并抛出 ArgumentException 的情况,并建议使用这些更简洁易读的静态方法替换。 这些方法不仅减少了样板代码,还为无效参数提供了一种一致的异常抛出方式。

public void Method(string arg1) { if (string.IsNullOrWhiteSpace(arg1)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(arg1)); }
public void Method(string arg1) { ArgumentException.ThrowIfNullOrWhiteSpace(arg1); }

快速修复会将 if 语句和 throw 表达式替换为根据原始检查内容选择的 ArgumentException.ThrowIfNullOrEmpty()ArgumentException.ThrowIfNullOrWhiteSpace() 单独调用。

2026年 3月 25日