Nullable Func <T, TResult>

In C # can I pass a method with a null value Func?

Neither works Func<A, bool>?nor Func?<A, bool>.

+5
source share
3 answers

This makes no sense.
All reference types, including Func<...>, may already be null.

Invalid types apply to value types ( structs), which usually cannot be null.

+17
source

A Func is a delegate that is a reference type. This means that it is already nullified (you can pass null to the method).

+3
source

Func → , ,

, (Action)

Action → , .

If you require Func to accept parameters that can be null (type NULL), or require Func to return a value that can be null (type NULL), there are no restrictions.

Example.

    Func<int?, int?, bool> intcomparer = 
    (a,b)=>
    {
    if(a.HasValue &&b.HasValue &&a==b) 
        return true;
        return false;
    } ;

    Func<int?, int?, bool?> nullintcomparer = 
    (a,b)=>
    {

    if(a.HasValue &&b.HasValue &&a==b) 
        return true;
    if(!a.HasValue || !b.HasValue)
       return null;
        return false;
    } ;

    var result1 = intcomparer(null,null); //FALSE
    var result2 = intcomparer(1,null); // FALSE
    var result3 = intcomparer(2,2); //TRUE


    var result4 = nullintcomparer(null,null); // null
    var result5 = nullintcomparer(1,null); // null
    var result6 = nullintcomparer(2,2); //TRUE
+1
source

All Articles