gpt4 book ai didi

c# - 传递变量以在C#中设置

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:43 25 4
gpt4 key购买 nike

我想将几个变量传递给函数并将其设置为其他内容,而不是从中读取。我计划在可以创建对象并将其添加到执行队列的情况下使用此方法。指针是否正确?

我知道我的问题的解释不好,但我不知道有更好的解释方法。

最佳答案

听起来您可能想要一个refout参数。例如:

public static void SetVariables(out int x, ref int y)
{
// Can't *read* from x at all before it's set
x = 10;
// Can read and write y
y++;
}

public static void Foo()
{
int setOnly;
int increment = 5;
SetVariables(out setOnly, ref increment);
Console.WriteLine("{0} {1}", setOnly, increment); // 10 6
}


有关更多信息,请参见我的 parameter passing article

关于c# - 传递变量以在C#中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11723278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com