惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - wzwyc

20260507随笔 在Zed中设置Xiaomi MiMo OxyPlot显示Legend图例 apache-iotdb-0.13.3-all-bin在WINDOWS 11系统上部署运行的问题 任务栏上的图标无法正常显示 应用启动时加载XML报错的问题处理 Prism的LoadedCommand命令没有被调用的问题 C#编译报错:可在所有平台上访问此调用站点。"xxxxxxxxxx" 仅在 "Windows" 7.0 及更高版本 上受支持。 C#系统下LINUX系统下组播数据的接收 消除只能在WINDOWS系统上使用的编译报警提示 WpfMultiStyle笔记 Kafka调研笔记 利用HtmlAgilityPack抓取网页的标题 关于Process的使用 Anaconda使用笔记 python下的plotly把图表转换成HTML 利用NAudio实现对音频设备的控制(静音、音量调节) 利用MathNet.Numerics求均方根 .NET 8以上版本,字节数组转字符串 .NET 8项目下载所有依赖到指定目录
异常的处理
wzwyc · 2025-10-31 · via 博客园 - wzwyc

新版本的C#支持nullable功能,如果一个可空的对象,没有进行空值判断的话,IDE会有报警提示。
借助下面的一些特性标识,能够进行null值的判断。

对函数内引用的属性进行异常判断

通过ArgumentNullException.ThrowIfNull。

private async void OnRestore()
{
    ArgumentNullException.ThrowIfNull(SelectedItems);

    var items = SelectedItems.Cast<UrlInfo>();
    items.ForEach(s => s.IsDeleted = false);
    await _helper.UpdateDataInfos(items);
    OnRefresh();
}

当返回值为true或false时,某个属性不为null

CanEdit返回值为true时,SelectedItem不为空。

[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(SelectedItem))]
private bool CanEdit()
{
    return SelectedItem != null;
}

如果想表示返回值为false时,SelectedItem不为空的话,修改为:

[System.Diagnostics.CodeAnalysis.MemberNotNullWhen(false, nameof(SelectedItem))]
private bool CanEdit()
{
    return SelectedItem != null;
}

当返回值为true或false时,传入的这个参数不为null

下面的代码,传入的参数判断结果为true的时候,就会标识为这个参数不为null

public static bool IsContainText([NotNullWhen(true)]string? value)
{
    return !string.IsNullOrEmpty(value);
}

同样,如何想传入的参数判断结果为false的时候,标识这个参数不为null的话,修改为:

public static bool IsContainText([NotNullWhen(false)]string? value)
{
    return !string.IsNullOrEmpty(value);
}