gpt4 book ai didi

c# - 我如何管理空检查的冲击?

转载 作者:IT王子 更新时间:2023-10-29 04:28:17 24 4
gpt4 key购买 nike

在编程中,我们经常遇到 null 检查出现特别多的情况。我说的是这样的事情:

if (doc != null)
{
if (doc.Element != null)
{
... and so on
}
else
throw new Exception("Element cannot be null");
} else {
throw new Exception("document cannot be null");
}

基本上,整个事情变成了一场难以理解的噩梦,所以我想知道:有没有更简单的方法来描述我在上面尝试做的事情? (除了 null 检查之外,我还时常得到类似 string.IsNullOrEmpty 的东西。)

接受的答案:我接受了有this link的答案因为所描述的方法是创新的,而这正是我想要的。谢谢肖恩!

最佳答案

查看这篇文章:A fluent approach to C# parameter validation

它是由一位 Paint.NET 开发人员编写的。他使用扩展方法来简化和清理空检查代码。

关于c# - 我如何管理空检查的冲击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/429112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com