我想重构一个方法,但我不太清楚如何,但我知道你可以做到.
我目前的方法:
public bool AdminShutdown(int accountID, int accountPin)
{
var status = false;
if (accountID == AdminLogin && accountPin == AdminPin)
{
status = true;
}
return status;
}
Run Code Online (Sandbox Code Playgroud)
我认为它应该是类似
var status = (accountID == AdminLogin) && (accountPin == AdminPin)但不起作用( Operator '&&' cannot be applied to operands of type 'bool' and 'int').
建议?
PS这段代码有用吗?
var tempReturnPerson = AccountHoldersList.Single((x => x.AccountNumber == accountId));
Run Code Online (Sandbox Code Playgroud)
代替:
public AccountHolders ReturnAccountInfo(int accountId)
{
//use linq to do this later
var returnPerson = new AccountHolders();
foreach (var person in AccountHoldersList)
{
if (accountId == person.AccountNumber)
{
returnPerson = person;
break;
}
}
return returnPerson;
}
Run Code Online (Sandbox Code Playgroud)
如果AdminLogin和AdminPin是int,那么
public bool AdminShutdown(int accountID, int accountPin)
{
return (accountID == AdminLogin && accountPin == AdminPin);
}
Run Code Online (Sandbox Code Playgroud)
你得到的错误信息让我觉得你可能用过=而不是用==
编辑
对于你的第二个问题
var tempReturnPerson = AccountHoldersList.Single((x => x.AccountNumber == accountId));
Run Code Online (Sandbox Code Playgroud)
应该是
var tempReturnPerson = AccountHoldersList.FirstOrDefault(x => x.AccountNumber == accountId) ?? new AccountHolders();
Run Code Online (Sandbox Code Playgroud)
如果您想要与方法相同的结果.