C# - 将 Switch 语句转换为 If-Else
C# - Convert Switch statement to If-Else
我在完成这项特定任务时遇到了一些麻烦。采用 switch 语句并将其转换为 if-else。该程序利用列表框 select 位置并显示相应的时区。
if (cityListBox.SelectedIndex != -1)
{
//Get the selected item.
city = cityListBox.SelectedItem.ToString();
// Determine the time zone.
switch (city)
{
case "Honolulu":
timeZoneLabel.Text = "Hawaii-Aleutian";
break;
case "San Francisco":
timeZoneLabel.Text = "Pacific";
break;
case "Denver":
timeZoneLabel.Text = "Mountain";
break;
case "Minneapolis":
timeZoneLabel.Text = "Central";
break;
case "New York":
timeZoneLabel.Text = "Eastern";
break;
}
}
else
{
// No city was selected.
MessageBox.Show("Select a city.");
因此,在大多数编程语言中,switch
语句和 if-else
语句几乎是同一个语句(一般来说;对于某些语言,在某些编译器上切换可能更快,并且我不确定特别是 C#)。 Switch
或多或少是 if-else
的语法糖。无论如何,与您的开关相对应的 if-else 语句看起来像这样:
if (city == "Honolulu") {
timeZoneLabel.Text = "Hawaii-Aleutian";
} else if (city == "San Francisco") {
timeZoneLabel.Text = "Pacific";
} else if (city == "Denver") {
timeZoneLabel.Text = "Mountain";
}
... etc
这有意义吗?
通过这种方法,您可以摆脱 switch
或 if-else
语句
创建一个 class 来表示时区
public class MyTimezone
{
public string City { get; set; }
public string Name { get; set; }
}
创建时区列表并将其绑定到列表框
var timezones = new[]
{
new MyTimezone { City = "Honolulu", Name = "Hawaii-Aleutian" },
new MyTimezone { City = "San Francisco", Name = "Pacific" },
// and so on...
}
cityListBox.DisplayMember = "City";
cityListBox.ValueMember = "Name";
cityListBox.DataSource = timezones;
然后在要使用选定时区的代码中
var selected = (MyTimeZone)cityListBox.SelectedItem;
timeZoneLabel.Text = selected.Name;
因为 Name
属性 用作 ValueMember
你可以使用 SelectedValue
属性.
// SelectedValue can bu null if nothing selected
timeZoneLabel.Text = cityListBox.SelectedValue.ToString();
我建议将 switch
变成 Dictionary<string, string>
,即分开 data(城市及其时区)和 representation(Label
、ListBox
等):
private static Dictionary<string, string> s_TimeZones = new Dictionary<string, string>() {
{"Honolulu", "Hawaii-Aleutian"},
{"San Francisco", "Pacific"},
//TODO: add all the pairs City - TimeZone here
};
然后你可以使用它如下(两个if
s):
if (cityListBox.SelectedIndex >= 0) {
if (s_TimeZones.TryGetValue(cityListBox.SelectedItem.ToString(), out string tz))
timeZoneLabel.Text = tz;
else
timeZoneLabel.Text = "Unknown City";
}
else {
// No city was selected.
MessageBox.Show("Select a city.");
...
我在完成这项特定任务时遇到了一些麻烦。采用 switch 语句并将其转换为 if-else。该程序利用列表框 select 位置并显示相应的时区。
if (cityListBox.SelectedIndex != -1)
{
//Get the selected item.
city = cityListBox.SelectedItem.ToString();
// Determine the time zone.
switch (city)
{
case "Honolulu":
timeZoneLabel.Text = "Hawaii-Aleutian";
break;
case "San Francisco":
timeZoneLabel.Text = "Pacific";
break;
case "Denver":
timeZoneLabel.Text = "Mountain";
break;
case "Minneapolis":
timeZoneLabel.Text = "Central";
break;
case "New York":
timeZoneLabel.Text = "Eastern";
break;
}
}
else
{
// No city was selected.
MessageBox.Show("Select a city.");
因此,在大多数编程语言中,switch
语句和 if-else
语句几乎是同一个语句(一般来说;对于某些语言,在某些编译器上切换可能更快,并且我不确定特别是 C#)。 Switch
或多或少是 if-else
的语法糖。无论如何,与您的开关相对应的 if-else 语句看起来像这样:
if (city == "Honolulu") {
timeZoneLabel.Text = "Hawaii-Aleutian";
} else if (city == "San Francisco") {
timeZoneLabel.Text = "Pacific";
} else if (city == "Denver") {
timeZoneLabel.Text = "Mountain";
}
... etc
这有意义吗?
通过这种方法,您可以摆脱 switch
或 if-else
语句
创建一个 class 来表示时区
public class MyTimezone
{
public string City { get; set; }
public string Name { get; set; }
}
创建时区列表并将其绑定到列表框
var timezones = new[]
{
new MyTimezone { City = "Honolulu", Name = "Hawaii-Aleutian" },
new MyTimezone { City = "San Francisco", Name = "Pacific" },
// and so on...
}
cityListBox.DisplayMember = "City";
cityListBox.ValueMember = "Name";
cityListBox.DataSource = timezones;
然后在要使用选定时区的代码中
var selected = (MyTimeZone)cityListBox.SelectedItem;
timeZoneLabel.Text = selected.Name;
因为 Name
属性 用作 ValueMember
你可以使用 SelectedValue
属性.
// SelectedValue can bu null if nothing selected
timeZoneLabel.Text = cityListBox.SelectedValue.ToString();
我建议将 switch
变成 Dictionary<string, string>
,即分开 data(城市及其时区)和 representation(Label
、ListBox
等):
private static Dictionary<string, string> s_TimeZones = new Dictionary<string, string>() {
{"Honolulu", "Hawaii-Aleutian"},
{"San Francisco", "Pacific"},
//TODO: add all the pairs City - TimeZone here
};
然后你可以使用它如下(两个if
s):
if (cityListBox.SelectedIndex >= 0) {
if (s_TimeZones.TryGetValue(cityListBox.SelectedItem.ToString(), out string tz))
timeZoneLabel.Text = tz;
else
timeZoneLabel.Text = "Unknown City";
}
else {
// No city was selected.
MessageBox.Show("Select a city.");
...