Tom Gullen
Tom Gullen

Reputation: 61737

Check Func<T> is not null without calling it

Given the class:

public class Options<T>
{
    protected internal Func<T> GetFromDB { get; set; }
}

How would I check that the GetFromDB method itself is not null, without calling the method? If I do:

if (options.GetFromDB() != null)
{
    var r = options.GetFromDB();
    ... do something
}

It appears to call code within the passed method twice, once for the null check and one for the actual call with return.

Upvotes: 3

Views: 3424

Answers (1)

Jim W
Jim W

Reputation: 5016

Would this not work?

if (options.GetFromDB != null)

Upvotes: 7

Related Questions