walterlv / BlogComments

3 stars 0 forks source link

post/cs8350-ref-arguments-combination-is-disallowed #138

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

CS8350:不允许使用“Foo(ref x, ref y)”的这种参数组合,因为它可能会在其声明范围之外公开由参数 x 引用的变量 - walterlv

标题所述的是一个 .NET/C# 程序的编译错误。这个编译错误是 C#7.2 时就引入的,但更新到 Visual Studio 2022(17.4) 后,有更多的情况会被判定为发生了此错误。 本文会解释这个错误的原因和解决办法。

https://blog.walterlv.com/post/cs8350-ref-arguments-combination-is-disallowed

Yue-cn commented 1 year ago

想问一下一个HttpClient的问题: 就比如在UI中点击一个按钮完成一个登陆操作(async) 然后客户端没有网络,点击登陆就会卡顿

每次点击会卡的更久,点了暂停发现是卡在PostAsync了 (整个程序把Task改为ValueTask了)

Yue-cn commented 1 year ago

image

dashenxian commented 1 year ago

可以编译呀,版本17.4.0

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var i = 0;
            var b = new Bar();
            Foo(ref i, ref b);

            Console.WriteLine("Hello, World!");
        }

        static void Foo(ref int i, ref Bar bar)
        {
        }

    }
    public ref struct Bar
    {
    }
}
dashenxian commented 1 year ago

使用顶级语句也可以

var i = 0;
var b = new Bar();
Foo(ref i, ref b);
static void Foo(ref int i, ref Bar bar)
{
}
public ref struct Bar
{
}