万一第一行代码被标记为无法访问但第二行不是,为什么?
First line of code in case is marked as unreachable but second line is not, why?
根据 Visual Studio 2015,第一个 case
语句中的第一行代码无法访问,但我不明白为什么。同一 case
语句中的第二行代码未标记为不可访问,并且 default
语句中的所有代码都是可访问的。 VS 是愚蠢还是我在这里遗漏了什么?
private static void LogToITSupport(string ErrorType)
{
var Email = new MailMessage();
Email.To.Add("");
Email.From = new MailAddress("");
switch ("ErrorType")
{
case "Database Connection":
Email.Subject = "JobSight Error, unable to connect to database.";
Email.Body = "JobSight is unable to connect to the JobSight database, this could indicate the databse is dow nor there is a server problem. Please investigate.";
break;
default:
Email.Subject = "JobSight has encountered an unknown error.";
Email.Body = "JobSight has encountered an unknown error and thinks that IT should fix it. Good Luck.";
break;
}
var Client = new SmtpClient("");
Client.Send(Email);
}
字符串文字 "ErrorType"
永远不会等于 "Database Connection"
所以编译器只是告诉你。
您可能想改用 ErrorType
变量:
switch (ErrorType)
{
case "Database Connection":
Email.Subject = "JobSight Error, unable to connect to database.";
Email.Body = "JobSight is unable to connect to the JobSight database, this could indicate the databse is dow nor there is a server problem. Please investigate.";
break;
default:
Email.Subject = "JobSight has encountered an unknown error.";
Email.Body = "JobSight has encountered an unknown error and thinks that IT should fix it. Good Luck.";
break;
}
现在如果 ErrorType
变量等于 "Database Connection"
那么第一条语句将被执行,否则执行默认语句。并且此评估将在运行时根据字符串变量的值完成。
回答你的实际问题。您正在打开文字字符串 "ErrorType" 并且您的选项是 "DataBase Connection" 或其他任何内容。
由于编译器查看的是文字字符串,它知道数据库连接永远不会出现这种情况,因此无法访问。
例如,如果您将开关更改为 "DataBase Connection",您会注意到第一行没问题,但随后您会在默认情况下收到该错误,因为编译器知道数据库连接是唯一的连接可以到达。
通过使用实际变量 switch(ErrorType),编译器不知道将传入什么,因此大小写和默认值都可能达到。
正如其他人所指出的那样,请删除引号,因为您想要打开变量 ErrorType 包含的字符串。
根据 Visual Studio 2015,第一个 case
语句中的第一行代码无法访问,但我不明白为什么。同一 case
语句中的第二行代码未标记为不可访问,并且 default
语句中的所有代码都是可访问的。 VS 是愚蠢还是我在这里遗漏了什么?
private static void LogToITSupport(string ErrorType)
{
var Email = new MailMessage();
Email.To.Add("");
Email.From = new MailAddress("");
switch ("ErrorType")
{
case "Database Connection":
Email.Subject = "JobSight Error, unable to connect to database.";
Email.Body = "JobSight is unable to connect to the JobSight database, this could indicate the databse is dow nor there is a server problem. Please investigate.";
break;
default:
Email.Subject = "JobSight has encountered an unknown error.";
Email.Body = "JobSight has encountered an unknown error and thinks that IT should fix it. Good Luck.";
break;
}
var Client = new SmtpClient("");
Client.Send(Email);
}
字符串文字 "ErrorType"
永远不会等于 "Database Connection"
所以编译器只是告诉你。
您可能想改用 ErrorType
变量:
switch (ErrorType)
{
case "Database Connection":
Email.Subject = "JobSight Error, unable to connect to database.";
Email.Body = "JobSight is unable to connect to the JobSight database, this could indicate the databse is dow nor there is a server problem. Please investigate.";
break;
default:
Email.Subject = "JobSight has encountered an unknown error.";
Email.Body = "JobSight has encountered an unknown error and thinks that IT should fix it. Good Luck.";
break;
}
现在如果 ErrorType
变量等于 "Database Connection"
那么第一条语句将被执行,否则执行默认语句。并且此评估将在运行时根据字符串变量的值完成。
回答你的实际问题。您正在打开文字字符串 "ErrorType" 并且您的选项是 "DataBase Connection" 或其他任何内容。
由于编译器查看的是文字字符串,它知道数据库连接永远不会出现这种情况,因此无法访问。
例如,如果您将开关更改为 "DataBase Connection",您会注意到第一行没问题,但随后您会在默认情况下收到该错误,因为编译器知道数据库连接是唯一的连接可以到达。
通过使用实际变量 switch(ErrorType),编译器不知道将传入什么,因此大小写和默认值都可能达到。
正如其他人所指出的那样,请删除引号,因为您想要打开变量 ErrorType 包含的字符串。