CS1988|C#无法在异步方法中使用ref,in,out类型的参数
🌀|场景:
BlazorServer的场景中推荐使用异步方法,使用ref,out,in为参数前缀则报错CS1988
原因如下:
ref
parameters are not supported inasync
methods because the method may not have completed when control returns to the calling code. Any changes to the referenced variables will not be visible to the calling code, resulting in a CS1988 error.
async
方法不支持ref
参数,因为当控件返回到调用代码时,该方法可能尚未完成。 对引用变量的任何更改都对调用代码不可见,从而导致 CS1988 错误。
⛵️|替代方法:
将需要用到的参数作为Task的返回,即
💥|延申:引用变量的可见性
1.当不使用基础类型为参数,使用引用类型时:
现有Blazor页面如下
@page "/login"
@using KatexTest2.Models
@using KatexTest2.Utils
@inject MyAuthProvider provider
<h3>LoginPage</h3>
<AuthorizeView>
<NotAuthorized>
@if(Isfailed){
<span>用户名或密码错误</span>
}
@if (test.Number==114.514M)
{
<span>压力吗室内</span>
}
<EditForm id = "LP" Model="loginModel" Context="Login">