扩展方法中的 C# out 参数

标签 c# extension-methods out

在扩展方法中,我收到一条错误消息,指出我的“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/

相关文章:

javascript - 在 JavaScript 中组织扩展方法的最佳实践

kotlin - 为什么在Kotlin中可以将高阶函数的扩展函数参数作为常规函数调用?

c# - 扩展方法优先级

C# - 如何传递对需要 out 变量的函数的引用?

.net - 如何使用输出参数调用 .net 方法?

mysql - mysql存储过程中真的有必要指定out参数吗?

c# - XAML 如何在派生的 UserControl 类中拥有 StaticResource

c# - 是否可以在 Azure 函数中保留环境设置?

c# - 使用凭据连接 ftp 服务器

c# - Asp.net MVC 休眠 : Can not modify more than one base table through a join view