问题描述
所以,我认为在当前的 C# 版本中是 C# 8.有几种方法可以检查引用类型是否不是 null
.我对下面这两个感到困惑:
So, I think in the current C# version which is C# 8. There are a few ways to check the reference types are not null
. I am confused by these two below:
o 是对象
o 是 {}
它们之间的主要区别是什么,我找不到关于它们的太多信息.
What's the main difference between them, I cannot find much info for them.
谁能给我解释一下?推荐哪一款?或者每种方法的优缺点是什么?
Can someone explain it to me? Which one is recommended? Or what are the pro and cons of each one?
推荐答案
o is object
是检查实例类型的原始方法(自 C# 1.0 起可用)
o is object
is the original way of checking the type of an instance (available since C# 1.0)
o is {}
使用与 C# 8.0 中引入的属性模式
{}
表示具有任何(或无)属性的对象实例,即任何对象实例.
{}
denotes an object instance with any (or none) properties, i.e. any object instance.
这篇关于C# 中 is object 和 is {} 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!