private void button1_Click(object sender, EventArgs e)
{
DialogResult str = MessageBox.Show("枚举返回值","AbortRetryIgnore,终止,重试,忽略", MessageBoxButtons.AbortRetryIgnore);
if (str.ToString() == "Abort") ;
{
MessageBox.Show("终止", "about");
}
if (str.ToString() == "Retry")
{
MessageBox.Show("重试", "retry");
}
if (str.ToString() == "ignore")
{
MessageBox.Show("忽略", "Ignore");
}
}
单击button后,根据返回值弹出消息框
{
DialogResult str = MessageBox.Show("枚举返回值","AbortRetryIgnore,终止,重试,忽略", MessageBoxButtons.AbortRetryIgnore);
if (str.ToString() == "Abort") ;
{
MessageBox.Show("终止", "about");
}
if (str.ToString() == "Retry")
{
MessageBox.Show("重试", "retry");
}
if (str.ToString() == "ignore")
{
MessageBox.Show("忽略", "Ignore");
}
}
单击button后,根据返回值弹出消息框
{
DialogResult result = MessageBox.Show("枚举返回值", "AbortRetryIgnore,终止,重试,忽略", MessageBoxButtons.AbortRetryIgnore);
switch (result)
{
case DialogResult.Abort:
MessageBox.Show("终止", "about");
break;
case DialogResult.Ignore:
MessageBox.Show("忽略", "Ignore");
break;
case DialogResult.Retry:
MessageBox.Show("重试", "retry");
break;
}
}
if (str.ToString() == "Abort") ; <- 多个分号
其实 可读性上来说 他这个场景上用if并不影响阅读.所以没必要非用switch
并且switch效率比if差.所以更没必要非用switch
3楼正解.