CS1988|C#无法在异步方法中使用ref,in,out类型的参数的问题

CS1988|C#无法在异步方法中使用ref,in,out类型的参数

🌀|场景:

请添加图片描述

BlazorServer的场景中推荐使用异步方法,使用ref,out,in为参数前缀则报错CS1988

原因如下:

ref parameters are not supported in async 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">
            <div>
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗马苏丹默罕默德

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值