5

In an extension method, I am receiving an error that my 'out' parameter does not exist in the current context. I assume this means that extension methods cannot have 'out' parameters, but this is not specified in the documentation. I would appreciate it if someone could please clarify!

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;
}
Dimpl
  • 935
  • 1
  • 10
  • 24

2 Answers2

7

Extension methods can have out parameters. You need to specify the type of your out parameter. So change code

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

to

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

and it should all work

dotnetom
  • 24,551
  • 9
  • 51
  • 54
0

You've missed specifying the type on the out parameter. It should read:

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

There is some conjecture you might be interested in on another question, regarding the readability of doing this kind of thing. Impossible to use ref and out for first ("this") parameter in Extension methods?

Community
  • 1
  • 1
James Decker
  • 109
  • 4