小编use*_*580的帖子

使用 ref 关键字传递给方法时,结构体是否被装箱?

有谁知道当使用 ref 关键字将结构传递给方法时是否会发生装箱

struct Test
{
    public int Value;
}

static void Main()
{
    Test test = new Test();
    SomeMethod(ref test);
    System.Diagnostics.Debug.WriteLine(test.Value);
}

static void SomeMethod(ref Test test)
{
    test.Value = 5;
}
Run Code Online (Sandbox Code Playgroud)

我想对类型使用结构,但我有几个方法需要更改它。如果它装箱,我只会将其传递并返回它,或者可能使用一个类来代替。

c# struct ref parameter-passing

5
推荐指数
1
解决办法
2371
查看次数

标签 统计

c# ×1

parameter-passing ×1

ref ×1

struct ×1