0

I'm trying to declare a Func<T, TResult> for a static method on a struct that has in parameters.

The signature of my method is something like this:

public static MyStruct MyMethod(in MyStruct input);

I tried to declare the Func this way:

new Func<MyStruct, MyStruct>(MyStruct.MyMethod);

But this causes an error:

"No overload for 'MyMethod' matches delegate Func<MyStruct, MyStruct>."

I did some googling, and found the following information in the Microsoft docs:

The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have one parameter that is passed to it by value, and that it must return a value. (https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=netcore-3.1)

From my code, I can see this to be true as I have some Funcs working properly for methods on this struct with in parameters, and they have another argument passed by value.

So my question has two parts, firstly why does at least one argument to a Func<> have to be passed by value? And secondly, how can I achieve getting a Func<> which will encapsulate the static method on my struct?

MattB
  • 159
  • 2
  • 8
  • 1
    Does it have to be a `Func<>`? Why not simply create your own [delegate](https://learn.microsoft.com/dotnet/csharp/programming-guide/delegates/using-delegates) to pass the method around? Something like `public delegate MyStruct MyFunc(in MyStruct input)`. -- `Func<>` delegates are basically just that and they are "prepared for your convenience". – Corak Jan 27 '20 at 15:37
  • 2
    I do not think this can be done with Func..... You need to create a delegate – Jonathan Alfaro Jan 27 '20 at 15:38
  • @Corak I suppose I could use a `delegate` but a `Func<>` would be much more neater and consistent with pre-existing code. Also I would quite like to know why it isn't possible with `Func<>` from a technical standpoint! But thanks for your comment, I'll be sure to fall back on it if required :) – MattB Jan 27 '20 at 15:39
  • You can use `Func` if you wrap the method inside a lambda: `new Func(x => MyStruct.MyMethod(x));` I do not know why this is necessary though. –  Jan 27 '20 at 15:40
  • `Func` was created before they added `in` I believe. – juharr Jan 27 '20 at 15:40
  • 1
    @juharr The parameter modifier `in` was added in C# 7.2. –  Jan 27 '20 at 15:40
  • 1
    @MattB - It's not possible with `Func<>` (atm?), because there is no `Func<>` defined like that. See [referencesource](https://referencesource.microsoft.com/#mscorlib/system/action.cs). For one or two parameters, the framework could provide `in` variants. But it get's pretty tedious pretty fast with more parameters (could be automated, but then you'd have billions of delegates in intellisense...). – Corak Jan 27 '20 at 15:45
  • @Amy Thanks! That has solved my immediate problem :) – MattB Jan 27 '20 at 15:48
  • 1
    @Selvin That's the wrong type of `in`. The OP is talking about this https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier – juharr Jan 27 '20 at 15:57
  • 1
    @Selvin This is a new usage of the `in` keyword, this time it's a parameter modifier rather than a generic modifier. –  Jan 27 '20 at 15:59
  • 2
    @MattB I have added the [tag:C#-7.3] tag since this is a language feature specific to that version. –  Jan 27 '20 at 16:02

0 Answers0