2

I have some code (not writted by me) that I don't understand:

private Func<ControllerContext, string> ThemeSelector { get; set; }

So this is not a type, this is Func < T, string > and I don't know how this kind of thing is named but I am unable to find some explanations on Google. Does someone can gives me some explanations of giving me a link to explain?

Thanks.

B Faley
  • 17,120
  • 43
  • 133
  • 223
Bronzato
  • 9,438
  • 29
  • 120
  • 212

4 Answers4

3

This is a delegate type, i.e. this is a property and it can be set. And it can be called as method.

for example

/* Action<String> is a delegate, get it as an object*/
public void Foo (String[] arr, Action<String> instruction) {
    foreach (var element in arr) {
        //use it as a method
        instruction(element);
    }
}

EDIT

The same applies to Func

// Func<Int32, Boolean> means that this is a function which accepts one argument as an integer and returns boolean.
public void Foo(Int32 data, Int32 otherData, Func<Int32, Boolean> instruction) {
    var result1 = instruction(data);
    var result2 = instruction(data2);
    Console.WriteLine(result1==result2);
}

EDIT

Even more comprehensive example.

//This methods accepts the third parameter as a function with 2 input arguments of type
// Int32 and return type String
public static void DoSomething(Int32 data1, Int32 data2, Func<Int32, Int32, String> whatToDo) {
    var result = whatToDo(data1, data2);
    Console.WriteLine(result);
}

public static Main(String[] args) {
    // here we are calling the DoSomething and
    // passing the Concat method as if it was an object.
    DoSomething(1, 2, Concat);
    // The same with multiply concat.
    DoSomething(7, 2, MultiplyConcat);
}
public static String Concat(Int32 arg1, Int32 arg2) {
    return arg1.ToString() + " " + arg2.ToString():
}
public static String MultiplyConcat(Int32 arg1, Int32 arg2) {
    return (arg1 * arg2).ToString();
}
Oybek
  • 7,016
  • 5
  • 29
  • 49
3

Func<ControllerContext, string> is a specific type of the generic Func<T,K>. You need to learn about generics first before you can fully understand that.

Therefore ThemeSelector is just a property of that type, which has setter and getter.

The Func type is a delegate type, which represents a function that takes one parameter of type T and returns an instance of type K.

This means that you can assign any such function to the property. E.g.:

private string MyFunction(ControllerContext context) {
    return "Hello world!";
}

...

ThemeSelector = MyFunction;

Console.WriteLine(ThemeSelector(null));

will print "Hello world!" in the console.

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
1

It is a type ! Actually it a delegate type. It represents a method that can be used without explictely declaring a delegate.

The type is a generic type. The one in your example is of type Func<T, TResult>, which represents a method that have one parameter of type T and returns a value of type TResult. Specifically to your example, it represents a method that have a ControllerContext parameter and returns a string value:

string ThemeSelectorHandler(ControllerContext context) {
    returns context.ToString();
}

Further reading:

Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
0

*A delegate is a pointer to a function.
*Func is a build-in delegate type. The last type in the angle brackets is the result type.
*Inheritance: Object-> Delegate-> Func
*Action is the build-in delegate pointing to a function without return.

Moon
  • 4,014
  • 3
  • 30
  • 66
sdfs
  • 1
  • 1