C#中不同的空值检查方式 [英] Different ways of null checking in C#
问题描述
问题是关于在C#中进行了两种不同的空检查方式
1:if(object == null )
2:if(null == object)
有些人说当我们有性能改进时使用2:就像编译器转换代码一样容易。但我不确定这些信息是否正确。我通常更喜欢1:
有没有什么好理由使用2:??
问候< br $> b $ b
我尝试了什么:
尝试了两种方式并找到了两种方法对我来说工作正常
生成的代码没有区别。它是变量和常量值之间的比较。当代码最终转换为汇编指令时,它将(根据CPU架构)将变量加载到寄存器中并与固定值进行比较(或者在常量值为零的特殊情况下检查寄存器是否为零) ,或者将内存位置与常数值进行比较。
我与C / C ++太相关而忘了条件必须评估为bool
。
[/ EDIT2]
但第二种形式有优势,如果你只是错误地输入一个等号:
// 为对象分配null,条件始终为false
如果( object = null )
{
}
// 编译器错误
如果( null = object )
{
}
使用对象
和null
不是这里最好的例子。
对于像int
这样的整数类型更有意义:
if ( 0 = num)
但是,这也应该生成一个关于始终为假的情况的警告。
[/ EDIT]
据我所知,第二个确实有轻微的性能提升。只需要看第二个对象是否为空而不是第一种方式加载实际对象然后与null进行比较。
我做不知道技术解释真的很好,但我也曾经学过它稍快一点。我很少这样写,因为它对我来说也不好读,所以它只取决于保存几微秒对你来说是否重要。
引用:很少说当我们使用2时有性能改进
这里是证明
随机运行以下代码5次,结果如下
obj == null
object obj = null;
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
if(obj == null){}
watch.Stop();
var a = watch.Elapsed.Ticks;
null == obj1
object obj1 = null;
System.Diagnostics.Stopwatch watch1 = new System.Diagnostics.Stopwatch();
watch1.Start();
if(null == obj1){}
watch1.Stop();
var b = watch1.Elapsed.Ticks;
结果
Elapsed Ticks obj == null null == obj1 37 0 32 2 27 0 135 18 37 9
Hi,
Question is regarding 2 different ways of null checking been done in C#
1: if(object == null)
2: if(null == object)
few says there are performance improvements when we use 2: like it will be easy for the compiler to convert the code. But i am not sure whether this information is correct. I normally prefer 1:
Is there any good reason to use 2:??
Regards
What I have tried:
Tried both the ways and found both works fine for me
There will be no difference in the generated code. It is a comparison between a variable and a constant value. When the code is finally converted to assembly instructions, it will (depending on the CPU architecture) load the variable into a register and compare with the fixed value (or in this special case where the constant value is zero check the register for being zero), or compare a memory location with a constant value.
[EDIT2]
I was too related to C/C++ and forgot that conditions must evaluate to abool
.
[/EDIT2]
But the second form has an advantage if you type only one equal sign by mistake:
// Assigns null to object, condition is always false if (object = null) { } // Compiler error if (null = object) { }
[EDIT]
Using anobject
andnull
is not the best example here.
It makes more sense for integral types likeint
:
if (0 = num)
However, this should also generate a warning about a condition that is always false.
[/EDIT]
It is my understanding that the second one does have a slight performance improvement. Something about only having to see if the second object is null or not rather than the first way it has to load the actual object and then compare against null.
I do not know the technical explanation real well but I had also learned once that it is slightly faster. I rarely write it that way because it does not read as well to me so it just depends on if saving a few microseconds is important to you or not.
Quote:few says there are performance improvements when we use 2
Yes here is the proof
randomly ran the following code for 5 times and the results as below
obj == null
object obj = null; System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); if (obj == null) { } watch.Stop(); var a = watch.Elapsed.Ticks;
null == obj1
object obj1 = null; System.Diagnostics.Stopwatch watch1 = new System.Diagnostics.Stopwatch(); watch1.Start(); if (null == obj1) { } watch1.Stop(); var b = watch1.Elapsed.Ticks;
Result
Elapsed Ticks obj == null null == obj1 37 0 32 2 27 0 135 18 37 9
这篇关于C#中不同的空值检查方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!