C# 并非所有代码路径都返回值
我得到一个错误不是所有的代码路径都返回一个值吗C# 并非所有代码路径都返回值,c#,C#,我得到一个错误不是所有的代码路径都返回一个值吗 public string Authentication(string studentID, string password) // this line? { var result = students.FirstOrDefault(n => n.StudentID == studentID); //find the StudentID that matches the string stude
public string Authentication(string studentID, string password) // this line?
{
var result = students.FirstOrDefault(n => n.StudentID == studentID);
//find the StudentID that matches the string studentID
if (result != null)
//if result matches then do this
{
//----------------------------------------------------------------------------
byte[] passwordHash = Hash(password, result.Salt);
string HashedPassword = Convert.ToBase64String(passwordHash);
//----------------------------------------------------------------------------
// take the specific students salt and generate hash/salt for string password (same way student.Passowrd was created)
if (HashedPassword == result.Password)
//check if the HashedPassword (string password) matches the stored student.Password
{
return result.StudentID;
// if it does return the Students ID
}
}
else
//else return a message saying login failed
{
return "Login Failed";
}
}
问题是,由于嵌套的if语句,第一个if语句不能确保返回值。假设您将结果设置为非null值,并且哈希密码和提供的密码不匹配,如果您按照该逻辑进行操作,则将无法命中return语句 您应该向嵌套的if语句添加else子句,如下所示:
if (HashedPassword == result.Password)
//check if the HashedPassword (string password) matches the stored student.Password
{
return result.StudentID;
// if it does return the Students ID
}
else
{
return "Login Failed";
}
或者更可取的是,删除您已有的else语句,以便函数以返回登录失败结束:
if (result != null)
{
//....
}
return "Login Failed";
…使用第二种方法,您无需担心使用else,因为如果满足所有其他条件,嵌套的return语句仍将结束函数。如果任何身份验证步骤失败,请尝试将此最终返回视为默认操作
代码中需要注意的另一点是,以这种方式返回混合数据不是理想的做法。i、 结果可能是学生ID,也可能是错误消息。考虑创建一个具有多个属性的专用结果类,调用代码可以检查这些属性,以查看逻辑验证的状态。类似以下内容的课程将是一个良好的开端:
public class LoginResult
{
//determines if the login was successful
public bool Success {get;set;}
//the ID of the student, perhaps an int datatype would be better?
public string StudentID {get;set;}
//the error message (provided the login failed)
public string ErrorMessage {get;set;}
}
尽管如此,如果结果不是空的,而是结果。密码!=HashedPassword您没有返回任何内容 您应该改为:
...
if (HashedPassword == result.Password)
{
return result.StudentID;
// if it does return the Students ID
}
return "Invalid Password";
...
取下另一个。照办
if(result != null) {
...
}
return "Login Failed";
如果出现以下情况,您还应归还一些物品:
if (HashedPassword != result.Password)
如果我对您的代码做了一些更改,请在内部添加一个else。试试看
public string Authentication(string studentID, string password)
{
var result = students.FirstOrDefault(n => n.StudentID == studentID);
var yourVar;
if (result != null)
{
byte[] passwordHash = Hash(password, result.Salt);
string HashedPassword = Convert.ToBase64String(passwordHash);
if (HashedPassword == result.Password)
{
//return result.StudentID;
yourVar = result.StudenID;
// if it does return the Students ID
}
}
else
//else return a message saying login failed
{
yourVar = "Login Failed";
}
return yourVar;
}
你的设计很难看,因为你把神奇的字符串登录失败和学生ID混为一谈。您可以使用null作为失败的返回值,或者将返回类型更改为更复杂的类型,可能是某种区分联合。您已经找到了问题的答案。不过,我建议您对这一点进行重构。与其将Login Failed作为字符串返回,不如返回null,或者返回一个表示登录是否成功的复杂类型,以及学生ID或null。可能的重复项将不起作用:1编译器无法推断var yourVar的类型,因此无法编译。2在当前没有返回路径、用户存在但密码错误的情况下,您仍然没有初始化您的VAR