好的,这里我有这个函数,在某些情况下可以返回空值。然而,即使在测试它是否之前,它仍然说它可能为空。
我的代码:
if (Account.FindAccount(usernameInput) == null) { return; }
if (Account.FindAccount(usernameInput).password == "1234") // warning CS8602: Dereference of a possibly null reference
{
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
该FindAccount操作是否保证幂等?编译器没有这样的保证。它在一次调用中返回的内容可能不会在另一次调用中返回。
更重要的是...如果您相信对于相同的输入它总是会返回相同的结果,为什么要调用它两次?只需调用一次并存储结果:
var account = Account.FindAccount(usernameInput);
if (account == null) { return; }
if (account.password == "1234")
{
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
111 次 |
| 最近记录: |