Question

I've seen in code such properties:

public static Func<string> TabMainDataToolTip
{
    get
    {
        return (Func<string>) (() => "Main Data");
    }
}

EDIT: in some places the actual string value is retrieved from application resources (localization).

What is the reason to return Func<T> instead of just type T? Does it give any advantage?

Was it helpful?

Solution 2

In this specific example there is no advantage, just extra overhead.

In general, this technique allows deferring the calculation of the final result until it is really needed. For example, some kinds of tooltips might want to include "expensive" information in their text -- perhaps the tooltip needs data that has to be fetched from a web service.

This scheme allows the property getter's caller to get hold of something that is "as good as" the final result without forcing them to evaluate that result on the spot. The caller could then decide to evaluate the result later, or evaluate it in parallel using a worker thread, or anything else it might want to do.

OTHER TIPS

Func<T> Encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter. You can use this delegate to represent a method, whereas T can be used to represent a type.

Func<TResult> is a predefined delegate in .NET.

A method conforming to this delegate will have return type TResult:

TResult someMethod(){}

The code you posted:

public static Func<string> TabMainDataToolTip
{
    get
    {
        return (Func<string>) (() => "Main Data");
    }
}

means that the property TabMainDataToolTip returns a method with return type of string.

In this case, said method is:

() => "Main Data"

equivalent to:

string anonymousMethod()
{
   return "Main Data";
}

There are actually 16 variants of the Func delegate, taking from 0 (zero) to 15 parameters and returning 1 result.

Example:

Func<T,TResult>

means a method like this:

TResult someMethod(T parameterName)
{
   return someTResultOject;
}

In addition to @Jon's answer, your code is consumed like this:

string mainData = TabMainDataToolTip();

Notice the parentheses. This is counter-intuitive for a property.

A better implementation would be based on Lazy<T>:

    private static readonly Lazy<string> TabMainDataToolTipFactory = new Lazy<string>(() => "Main Data");

    public static string TabMainDataToolTip
    {
        get
        {
            return TabMainDataToolTipFactory.Value;
        }
    }

Usage example:

string mainData = TabMainDataToolTip;

In addition to other answers, there are times when you may need some info/state at the time the Func is called. For example, you may need the DateTime.Now when the Func is called. You also may need to do some additional work. For example, if it is Tuesday, return one string otherwise return another.

Another reason you may want a Func is because in some cases it may be called but in other cases it may never be called and thus the code within the Func may never be executed.

Func<T> is just like any other method which will return T. In your case string.

This will help you to pass the entire method as a parameter to any other method.

You can find more info on this delegate here at MSDN

Most people prefer using Func<T> , if they are developing any API.

But the above sample makes no sense.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top