我尝试将下面的代码转换为switch语句,但我的技能在c#中并不完美。有人可以帮我解决这个案子吗?
protected void logobtn_Click(object sender, ImageClickEventArgs e)
{
HttpCookie cookie = Request.Cookies.Get("Location");
if (cookie["Location"] == null ||
cookie["Location"].ToString() == null ||
cookie["Location"].ToString() == "" ||
cookie["Location"].ToString() == "-- Europe and Eastern Europe --" ||
cookie["Location"].ToString() == "-- Asia & South-East Asia --" ||
cookie["Location"].ToString() == "-- North & South of America --"
) {
Response.Redirect("Index.aspx");
} else {
Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value));
}
}
答案 0 :(得分:2)
如果您使用C#6
,可以这样做switch (cookie["Location"]?.ToString()) {
case null:
case "":
case "-- Europe and Eastern Europe --":
case "-- Asia & South-East Asia --":
case "-- North & South of America --":
Response.Redirect("Index.aspx");
break;
default:
Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value));
break;
}
当?.ToString()
为null
时,cookie["Location"]
内容会返回null
。因此,处理这两个条件:
cookie["Location"] == null ||
cookie["Location"].ToString() == null
其他人非常直截了当。我想你可以理解。
IMO,开关看起来不如原来的那样好。在这种情况下,if可能更容易理解。但如果你真的喜欢转换,那也很好。
答案 1 :(得分:1)
您可以尝试以下代码:
HttpCookie cookie = Request.Cookies.Get("Location");
if (cookie["Location"]==null)
Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value));
else
switch (cookie["Location"].ToString())
{
case null:
case "":
case "-- Europe and Eastern Europe --":
case "-- Asia & South-East Asia --":
case "-- North & South of America --":
Response.Redirect("Index.aspx");
break;
default:
Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value));
break;
}
答案 2 :(得分:1)
试试这个,
HttpCookie cookie = Request.Cookies.Get("Location");
switch (cookie["Location"])
{
case null :
case "":
case "--Europe and Eastern Europe--" :
case "-- Asia & South-East Asia --":
case "-- North & South of America --":
Response.Redirect("Index.aspx");
break;
default:
Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value));
break;
}
答案 3 :(得分:0)
我认为你不需要修改你的条件来切换,如果你真的想要你可以使用下面的代码。
string coo = Convert.ToString(Request.Cookies["Location"]==null?null:Request.Cookies["Location"].Value);
switch(coo)
{
case null:
Response.Redirect("Index.aspx");
case "":
Response.Redirect("Index.aspx");
case "-- North & South of America --":
Response.Redirect("Index.aspx");
case "-- Asia & South-East Asia --":
Response.Redirect("Index.aspx");
case "-- Europe and Eastern Europe --":
Response.Redirect("Index.aspx");
default:
Response.Redirect(string.Format("Berava.aspx?Country={0}", coo));
}
答案 4 :(得分:0)
var cookie = Request.Cookies.Get("Location")?["Location"];
switch (cookie)
{
case null:
Response.Redirect("Index.aspx");
// DO STUFF
break;
case "":
// DO STUFF
break;
case "-- Europe and Eastern Europe --":
// DO STUFF
break;
case "-- Asia & South-East Asia --":
// DO STUFF
break;
case "-- North & South of America --":
// DO STUFF
break;
default:
Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value));
break;
}
答案 5 :(得分:-1)
如果你肯定想要一个案例陈述,那么这应该有效:
HttpCookie cookie = Request.Cookies.Get("Location");
if (cookie != null)
{
string cookieString = cookie["Location"].ToString();
switch (cookieString)
{
case "":
case "-- Europe and Eastern Europe --":
case "-- Asia & South-East Asia --":
case "-- North & South of America --":
Response.Redirect("Index.aspx");
break;
default:
Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value));
break;
}
}
else
{
//this is the null case...
Response.Redirect("Index.aspx");
}