C#:这怎么能假设? [英] C#: how can this assume things?
问题描述
using System;
static class boolclass
{
public static void Main()
{
int a = 1;
int b = 2;
// Which one is greater?
bool greaterAB = (a > b);
// Is 'a' equal to 1?
bool equalA1 = (a == 1);
// Print the results on the console
if (greaterAB )
{
Console.WriteLine("A > B");
}
else
{
Console.WriteLine("A <= B");
}
Console.WriteLine("greaterAB = " + greaterAB);
Console.WriteLine("equalA1 = " + equalA1);
// Console output:
// A <= B
// greaterAB = False
// equalA1 = True
Console.ReadKey(true);
}
}
这一点---
This point---
if (greaterAB )
是否假设
Does it assume that
if (greaterAB)
是
is
if (greaterAB == True)
更多或者更少的布尔默认值是false,所以即使假定这个,它也应该假设 -
More or less boolean default value is false so even if it assumes this, it should assume--
if (greaterAB == False)
我尝试了什么:
谷歌搜索它但我找不到任何我能理解或与之相关的东西。
What I have tried:
Googled it but I did not find anything which I could understand or is related to this.
推荐答案
if (greaterAB)
和
and
if (greaterAB == true)
是等价的,第一个版本应该是首选。
在C#中,您无法使用未初始化的标识符,实际上您初始化 \\ n \\ n \\ n \\ n \\ n \\ n $ $
:
are equivalent and the first version should be preferred.
In C# you can't use an un-initialized identifier and in fact you initialize greaterAB
:
bool greaterAB = (a > b);
所以在这一点上它反映了 a
是否大于 b
以及布尔值的默认值 false
与所有这些无关。现在,如果您已将 greaterAB
声明为类变量(这没有多大意义),那么它将被初始化为默认值 false
第一次使用静态类,或者如果它是非静态类,则实例化该类的对象。
So at this point it reflects the fact whether a
is greater than b
and a boolean's default value of false
has nothing to do with all this. Now if you had declared greaterAB
as a class variable (which wouldn't make much sense) then it would be initialized to the default value of false
the first time the static class is being used or, if it's a non-static class, an object of that class is instantiated.
这篇关于C#:这怎么能假设?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!