gpt4 book ai didi

c# - 为什么编译器将 bool 转换为整数并返回 bool 而不是返回 bool 本身?

转载 作者:太空宇宙 更新时间:2023-11-03 17:39:09 26 4
gpt4 key购买 nike

我正在阅读 VideoFileWriter来自 AForge.Video.FFMPEG 的类(class)通过 ILSPY 组装(我很想看看特定方法是如何工作的)并发现了这个:

public bool IsOpen {
[return: MarshalAs(UnmanagedType.U1)]
get {
return ((this.data != null) ? 1 : 0) != 0;
}
}

将 bool 转换为整数而不是返回 bool 转换的原因是什么? ?

最佳答案

它是反编译的代码,很可能只是反编译器的一个小故障。

经过一番思考,这是一个合理的实现,可能会变成相同的编译代码

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;
}
}

关于c# - 为什么编译器将 bool 转换为整数并返回 bool 而不是返回 bool 本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37957870/

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