Can a method in C# return a method?
A method could return a lambda expression for example, but I don't know what kind of type parameter could I give to such a method, because a method isn't Type
. Such a returned method could be assigned to some delegate.
Consider this concept as an example:
public <unknown type> QuadraticFunctionMaker(float a , float b , float c)
{
return (x) => { return a * x * x + b * x + c; };
}
delegate float Function(float x);
Function QuadraticFunction = QuadraticFunctionMaker(1f,4f,3f);
The Types you are looking for are Action<>
or Func<>
.
The generic parameters on both types determine the type signature of the method. If your method has no return value use Action
. If it has a return value use Func
whereby the last generic parameter is the return type.
For example:
public void DoSomething() // Action
public void DoSomething(int number) // Action<int>
public void DoSomething(int number, string text) // Action<int,string>
public int DoSomething() // Func<int>
public int DoSomething(float number) // Func<float,int>
public int DoSomething(float number, string text) // Func<float,string,int>