5
public class Foo
{
    public void DoFoo()
    {
       int x;
       var coll = TheFunc("bar", out x);
    }

    public Func<string, int, ICollection<string>> TheFunc { get; set; }
}

Error: "Argument 2 should not be passed with the 'out' keyword."

public class Foo
{
    public void DoFoo()
    {
       int x;
       var coll = TheFunc("bar", out x);
    }

    public Func<string, out int, ICollection<string>> TheFunc { get; set; }
}

Error: "Invalid variance modifier. Only interface and delegate type parameters can be specified as variant."

How do I get an out parameter in this function?

michael
  • 14,844
  • 28
  • 89
  • 177
  • see http://stackoverflow.com/questions/1365689/cannot-use-ref-or-out-parameter-in-lambda-expressions for some details – shelleybutterfly Jul 22 '11 at 20:32
  • 1
    FYI the more general rule here is that *a type argument must be a type which is convertible to object*. "out int" is not convertible to object; you cannot convert "reference to an int variable" to object. – Eric Lippert Jul 22 '11 at 20:35

2 Answers2

8

Define a delegate type:

public delegate ICollection<string> FooDelegate(string a, out int b);

public class Foo
{
    public void DoFoo()
    {
       int x;
       var coll = TheFunc("bar", out x);
    }

    public FooDelegate TheFunc { get; set; }
}
cdhowie
  • 158,093
  • 24
  • 286
  • 300
7

You need to make your own delegate:

delegate ICollection<string> MyFunc(string x, out int y);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964