[英]What is the difference between these two following approaches?
I have following code snippet. 我有以下代码片段。
bool b = false;
if (b) {}
but I have seen many people write something like: 但是我已经看到很多人写这样的东西:
if (true == b){}
They both look same to me, is there any difference here ? 他们对我来说都一样,这有什么区别吗?
bool b;
if (b) {}
You can't use this because C# compiler doesn't allow to use unassigned local variables. 您不能使用它,因为C#编译器不允许使用未分配的局部变量。
On the other hand, there is no difference between 另一方面,两者之间没有区别
bool b = true;
if (b) {}
and 和
bool b = true;
if (true == b){}
They generate same MSIL code as well. 它们也生成相同的MSIL代码。 But in my opinion, equality checking in second example is unnecessary.
但是我认为,第二个示例中的相等性检查是不必要的。 That's why
if(b)
seems cleaner than if(true == b)
. 这就是为什么
if(b)
看起来比if(true == b)
干净的原因。
No, there is not a difference. 不,没有区别。 (well, apart from the number of typed letters i suppose).
(嗯,除了我想输入的字母的数量)。
bool b = true; //ensure you initialise it though!! (if it was **C** you wouldn't think not to!)
So; 所以;
if(b)
{
//doSomthing...
}
means 手段
if(true)
{
//doSomething...
}
However, 然而,
if b was assigned to false, then the if statement would be skipped; 如果将b赋值为false,则将跳过if语句; like so:
像这样:
bool b = false;
if(b)
{
//this won't be executed since b is false
}
As for your question, though, with you initialising b at the beginning, then yes, they would be the same 至于您的问题,尽管在开始时初始化b,然后是的,它们是相同的
For me 为了我
if (true == b){}
is quite inelegant. 相当优雅。 Following that style in place of
遵循这种风格代替
if (a == b){}
you should put 你应该把
if ((a == b) == true){}
and so on.... 等等....
The if
statement needs a boolean expression (the condition) to be true
in order to execute the instructions inside its block. if
语句需要一个布尔表达式(条件)为true
才能执行其块内的指令。 If you compare a boolean value to another boolean value you still get a boolean result, but, unless you are comparing two boolean variables with an unknown value, you may be writting a piece of useless code. 如果将一个布尔值与另一个布尔值进行比较,您仍然会得到一个布尔结果,但是,除非将两个具有未知值的布尔变量进行比较,否则可能会编写一段无用的代码。 To explain myself better:
为了更好地说明自己:
bool a, b;
// some computation
if(a == b)
{
// do stuff
}
makes sense, while 有道理,而
bool a;
// some computation
if(a == true)
{
// do stuff
}
contains some useless code (in my opinion, it even loses some readability) because it is the equivalent of: 包含一些无用的代码(在我看来,它甚至失去了一些可读性),因为它等效于:
bool a;
// some computation
if(a)
{
// do stuff
}
For the code: 对于代码:
bool b = true;
if (b)
Console.WriteLine("b-ok");
if (b==true)
Console.WriteLine("b-true");
if we check the IL-CODE for the the first if 如果我们检查IL-CODE为第一,如果
IL_000b: ldloc.0
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: stloc.1
IL_0010: ldloc.1
IL_0011: brtrue.s IL_001e
IL_0013: ldstr "b-ok"
IL_0018: call void [mscorlib]System.Console::WriteLine(string)
IL_001d: nop
and for the second if 第二, 如果
IL_001e: ldloc.0
IL_001f: ldc.i4.0
IL_0020: ceq
IL_0022: stloc.1
IL_0023: ldloc.1
IL_0024: brtrue.s IL_0031
IL_0026: ldstr "b-true"
IL_002b: call void [mscorlib]System.Console::WriteLine(string)
IL_0031: ....
We can clearly see that C# compiler produces the same IL code for both statements. 我们可以清楚地看到C#编译器为这两个语句生成相同的IL代码。 Thus both approaches are totally same.
因此,两种方法完全相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.