gpt4 book ai didi

扩展方法中的 C# out 参数

转载 作者:太空狗 更新时间:2023-10-30 01:02:50 25 4
gpt4 key购买 nike

在扩展方法中,我收到一条错误消息,指出我的“out”参数在当前上下文中不存在。我假设这意味着扩展方法不能有“out”参数,但这在文档中没有指定。如果有人能澄清一下,我将不胜感激!

public static int customMax(this int[] data, out index)
{
int max = data[0];
index = 0;

for (int i = 1; i < data.Length; i++) {
if (data[i] > max) {
max = data[i];
}
}

return max;
}

最佳答案

扩展方法可以有 out 参数。您需要指定输出参数的类型。所以改代码

public static int customMax(this int[] data, out index)

public static int customMax(this int[] data, out int index)

它应该一切正常

关于扩展方法中的 C# out 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163794/

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