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,364
  • 27
  • 86
  • 169
  • 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
  • 144,362
  • 22
  • 272
  • 285
7

You need to make your own delegate:

delegate ICollection<string> MyFunc(string x, out int y);
SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933