我开始学习可空类型并遇到以下行为。
在尝试nullable int时,我看到比较运算符给了我意想不到的结果。例如,在我的下面的代码中,我得到的输出是"两者和1相等" 。注意,它不会打印" null"同样。
int? a = null;
int? b = 1;
if (a < b)
Console.WriteLine("{0} is bigger than {1}", b, a);
else if (a > b)
Console.WriteLine("{0} is bigger than {1}", a, b);
else
Console.WriteLine("both {0} and {1} are equal", a, b);
我希望任何非负整数都大于null,我在这里错过了什么?
答案 0 :(得分:167)
根据MSDN - 它位于“操作员”部分的页面下方:
当您对可空类型进行比较时,如果其中一个可空类型的值为
null
而另一个不可归类型,则除了false
之外,所有比较都会计算为!=
因此,a > b
和a < b
评估为false
,因为a
为空...
答案 1 :(得分:34)
正如MSDN所说
当您与可空类型进行比较时,如果值为1 可以为空的类型是null,而另一个不是,所有的比较 评估为假,除了!=(不等于)。重要的是不要 假设因为特定比较返回false,所以 相反的情况返回true。在以下示例中,10不是 大于,小于,等于null。只有num1!= num2 评估为真。
int? num1 = 10;
int? num2 = null;
if (num1 >= num2)
{
Console.WriteLine("num1 is greater than or equal to num2");
}
else
{
// This clause is selected, but num1 is not less than num2.
Console.WriteLine("num1 >= num2 returned false (but num1 < num2 also is false)");
}
if (num1 < num2)
{
Console.WriteLine("num1 is less than num2");
}
else
{
// The else clause is selected again, but num1 is not greater than
// or equal to num2.
Console.WriteLine("num1 < num2 returned false (but num1 >= num2 also is false)");
}
if (num1 != num2)
{
// This comparison is true, num1 and num2 are not equal.
Console.WriteLine("Finally, num1 != num2 returns true!");
}
// Change the value of num1, so that both num1 and num2 are null.
num1 = null;
if (num1 == num2)
{
// The equality comparison returns true when both operands are null.
Console.WriteLine("num1 == num2 returns true when the value of each is null");
}
/* Output:
* num1 >= num2 returned false (but num1 < num2 also is false)
* num1 < num2 returned false (but num1 >= num2 also is false)
* Finally, num1 != num2 returns true!
* num1 == num2 returns true when the value of each is null
*/
答案 2 :(得分:22)
总结:即使两个操作数均为空,任何与null(salt-cp 'test1' /tmp/test* salt:// -l debug
[DEBUG ] Reading configuration from /etc/salt/master
[DEBUG ] Configuration file path: /etc/salt/master
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[DEBUG ] Reading configuration from /etc/salt/master
[DEBUG ] Missing configuration file: /root/.saltrc
[DEBUG ] MasterEvent PUB socket URI: /var/run/salt/master/master_event_pub.ipc
[DEBUG ] MasterEvent PULL socket URI: /var/run/salt/master/master_event_pull.ipc
[DEBUG ] Initializing new AsyncZeroMQReqChannel for ('/etc/salt/pki/master', 'XXX_master', 'tcp://127.0.0.1:44506', 'clear')
[DEBUG ] Initializing new IPCClient for path: /var/run/salt/master/master_event_pub.ipc
[DEBUG ] SaltReqTimeoutError, retrying. (1/3)
[DEBUG ] SaltReqTimeoutError, retrying. (2/3)
[DEBUG ] SaltReqTimeoutError, retrying. (3/3)
[ERROR ] An un-handled exception was caught by salt's global exception handler:
SaltClientError: Salt request timed out. The master is not responding. If this error persists after verifying the master is up, worker_threads may need to be increased.
Traceback (most recent call last):
File "/usr/bin/salt-cp", line 10, in <module>
salt_cp()
File "/usr/lib/python2.7/dist-packages/salt/scripts.py", line 359, in salt_cp
client.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/cp.py", line 38, in run
cp_.run()
File "/usr/lib/python2.7/dist-packages/salt/cli/cp.py", line 105, in run
ret = local.cmd(*args)
File "/usr/lib/python2.7/dist-packages/salt/client/__init__.py", line 568, in cmd
**kwargs)
File "/usr/lib/python2.7/dist-packages/salt/client/__init__.py", line 317, in run_job
raise SaltClientError(general_exception)
SaltClientError: Salt request timed out. The master is not responding. If this error persists after verifying the master is up, worker_threads may need to be increased.
salt.exceptions.SaltClientError: Salt request timed out. The master is not responding. If this error persists after verifying the master is up, worker_threads may need to be increased.
,>=
,<
,<=
)的不等式比较都会返回>
。即。
false
与null(null > anyValue //false
null <= null //false
,==
)的任何相等或不相等比较都“按预期”工作。即。
!=
答案 3 :(得分:1)
将C#与SQL进行比较
C#:a = null和b = null => a == b => true
SQL:a = null和b = null => a == b =>假