C#短路怪诞
给定a=null 这不应该引发异常吗C#短路怪诞,c#,C#,给定a=null 这不应该引发异常吗 if(a != null && ((string)a).ToLower()=="boo"){ ... operation } 由于延迟求值,不应调用第二条语句,因此((字符串)a).ToLower()不应引发异常,对吗 更新 IOrderedEnumerable<SPListItem> Items = sourceList.GetItems("ProjectID", "Title", "ProjectName", "Feat
if(a != null && ((string)a).ToLower()=="boo"){
... operation
}
由于延迟求值,不应调用第二条语句,因此((字符串)a).ToLower()不应引发异常,对吗
更新
IOrderedEnumerable<SPListItem> Items = sourceList.GetItems("ProjectID", "Title", "ProjectName", "Featured", "Size", "Description")
.Cast<SPListItem>().AsEnumerable().OrderBy((i => rnd.Next()));
SPListItemCollection randProjImages = SPContext.Current.Web.Lists.TryGetList("Random Projects Images").GetItems();
var randomImgNrs = Enumerable.Range(0, randProjImages.Count).OrderBy(i => rnd.Next()).ToArray();
Dictionary<string, string> projectImages = new Dictionary<string,string>();
IEnumerable<SPListItem> projImages = SPContext.Current.Web.Lists.TryGetList("Assigned Projects Images").
GetItems().Cast<SPListItem>().AsEnumerable();
foreach (SPListItem it in projImages) projectImages.Add((string)it["ProjectID"], (string)it["ServerUrl"]);
int qCount = 0;
foreach (SPListItem item in Items) {
if (item["Size"] != null && item["Featured"]!=null &&
((string)item["Size"]).ToLower() == "big" || ((string)item["Featured"]).ToLower() == "yes") {
dataItems.Add(new Project(item["ProjectID"].ToString(), (string)item["Title"],
"/sites/Galileo/SitePages/" + Utils.getCurrentLang().ToLower() + "/Projects.aspx#id=pwp" + item["ProjectID"],
projectImages[(string)item["ProjectID"]],
Utils.truncateString(item.Fields["Description"].GetFieldValueAsText(item["Description"]), 175), "project"));
}else{
并在调试器中的if语句之前中断,k==null
String.IsNullOrWhiteSpace(a)
确切地说,这不会引发异常。由于表达式是
和
,仅计算第一部分就足以知道它永远不会计算为true,因此跳过第二部分。正确!(除了它不是惰性评估)
如果第一个操作数的计算结果为true,则不计算第二个操作数
没有例外:
string a = null;
if (a != null && ((string) a).ToLower() == "boo")
string a = "";
if (a != null && ((string) a).ToLower() == "boo")
string a = "boo";
if (a != null && ((string) a).ToLower() == "boo")
这是应该的(尽管铸造是不必要的)
不编译:
T a = new T();
if (a != null && ((string) a).ToLower() == "boo")
T? a = null;
if (a != null && ((string) a).ToLower() == "boo")
T? a = new T();
if (a != null && ((string) a).ToLower() == "boo")
其中T
是由于从T
转换为string
而产生的string
以外的任何类型真正的问题是:为什么认为它会引发异常?如果第一个参数为false(
a
为null
),则不会计算&&的第二个参数,这是正确的。因此代码不能抛出NullReferenceException。但是,如果a
不是null
,而是不能强制转换为string
的对象,则代码可以抛出InvalidCastException
if(a != null && ((string)a).ToLower()=="boo"){
... operation
}
请注意,以不区分大小写的方式比较字符串的最佳做法是使用:
由于该方法是静态的,因此不需要空检查
如果(项目[“大小”]!=null和项目[“特色”]!=null&&
((字符串)项[“大小]).ToLower()=“大”|;((字符串)项[“特色”]).ToLower()=“是”)
它转到((字符串)项[“特色”])。ToLower()=“是”)您确定
a
为空吗?它是可空类型吗?有什么例外?如果是字符串,请使用!string.IsNullOrEmpty(a)而不是a!=null为什么需要将“a”强制转换为字符串?它是什么类型的?变量?多给点context@haknick:向我们展示真实的代码。异常是NullReferenceException
,因此OP提供的代码显然不是真实的代码。我知道我可以做到这一点,但我很好奇知道为什么会发生上述情况?哇。。。多么愚蠢的失误,这就是为什么连续编码超过15小时是不好的
if (String.Equals(a, "foo", StringComparison.CurrentCultureIgnoreCase))
{
// ... operation
}