[英]Converting C assign/conditional statement to C#
I am translating some code from C to C#. 我正在将一些代码从C转换为C#。 I'm not sure how best to translate the following 2 lines:
我不确定如何最好地翻译以下两行:
if (tt = v >> 16)
{
r = (t = tt >> 8) ? 24 + LT[t] : 16 + LT[tt];
}
tt, v, and t are ulongs (not really relevant to the problem). tt,v和t是ulong(与问题无关)。 The problem is I don't think C# allows the assign/conditional operation in one statement.
问题是我不认为C#在一条语句中允许赋值/条件操作。
In C#, one cannot implicitly convert from ulong to bool. 在C#中,不能将ulong隐式转换为bool。 The following line doesn't compile either:
以下行也不编译:
if ((bool)(tt = v >> 16))
Here is the one for your if
statement. 这是供您
if
语句使用的语句。
(tt = v >> 16) != 0
You cant easily cast an int
to a bool
. 您不能轻易将
int
转换为bool
。
This is a direct conversion: 这是直接转换:
tt = v >> 16;
if (tt != 0) {
t = tt >> 8;
r = (t != 0) ? 24 + LT[t] : 16 + LT[tt];
}
Generally speaking, terse C code doesn't look good when converted to C#. 一般来说,简洁的C代码在转换为C#时看起来并不好。 I suggest making it a little bit more verbose to make life easier in the future.
我建议使它更加冗长一些,以使将来的生活更轻松。 (Call me biased, but it takes a lot more to frighten people used to C than those using newer languages).
(告诉我有偏见,但是与使用较新语言的人相比,使习惯于C的人更容易受到惊吓)。
Try this: 尝试这个:
tt = v >> 16;
if (tt != 0)
This shoud simply work: 这应该工作:
tt = v >> 16;
if (tt != 0)
{
t = tt >> 8;
r = (t != 0) ? 24 + LT[t] : 16 + LT[tt];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.