[英]Why does the compiler convert bool to integer and back to bool instead of returning the bool itself?
I was reading VideoFileWriter
class from AForge.Video.FFMPEG
assembly via ILSPY (I was interested to see how a particular method works) and found this: 我正在通过VideoFileWriter
从AForge.Video.FFMPEG
汇编中读取VideoFileWriter
类(我很想知道特定方法的工作原理),并发现了这一点:
public bool IsOpen {
[return: MarshalAs(UnmanagedType.U1)]
get {
return ((this.data != null) ? 1 : 0) != 0;
}
}
What's the reason to do that bool to integer than back to bool conversion rather just do this.data != null
? 将布尔值转换为整数而不是转换为布尔值转换的原因是什么,而不是仅仅这样做this.data != null
?
It is decompiled code, it is likely just a glitch of the decompiler. 它是反编译的代码,很可能只是反编译器的故障。
After thinking for a bit, here is a reasonable implementation that may potentially turn in to the same compiled code 经过一番思考,这是一个合理的实现,可能会变成相同的已编译代码
public enum ConnectionState
{
Closed = 0,
Open = 1,
Opening = 2,
OtherStuff = 3,
AndSoOn = 4,
}
public bool IsOpen
{
get
{
ConnectionState state;
if (this.data != null)
{
state = ConnectionState.Open;
}
else
{
state = ConnectionState.Closed;
}
return state != ConnectionState.Closed;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.