在C#基本布尔逻辑 [英] Basic boolean logic in C#
问题描述
这是下面的好?
this.isLoggedIn = (bool)HttpContext.Current.Session["li"] == true;
或
this.isLoggedIn = (bool)HttpContext.Current.Session["li"];
它需要是真实的,只有当会话是真实的。如果会话设置为false将这个评价为true#2为它的存在?抑或是评估它的价值?
It needs be to true ONLY when the session is true. If the session is set to false will this evaluate to true in #2 as it exists? Or is it evaluating its value?
推荐答案
第二个:
this.isLoggedIn = (bool)HttpContext.Current.Session["li"];
(布尔)HttpContext.Current.Session [礼]
已经是一个布尔值(所以会为真正
或假
),因此无需为布尔前pression额外的比较和返回值。
(bool)HttpContext.Current.Session["li"]
is already a boolean (so will be either true
or false
), so no need for the extra comparison and return value of the boolean expression.
无论哪种方式,你需要检查里
会话变量尝试投放之前存在,或者您的code将抛出(我觉得的NullReferenceException
)。
Either way, you need to check that the li
session variable exists before trying to cast it, or your code will throw (I think a NullReferenceException
).
这篇关于在C#基本布尔逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!