Can someone explain what the C# “Func” does?

后端 未结 8 2396
情话喂你
情话喂你 2020-12-13 12:34

I\'m reading the Pro MVC 2 book, and there is an example of creating an extension method for the HtmlHelper class.

Here the code example:

public stat         


        
8条回答
  •  一生所求
    2020-12-13 13:40

    I have implemented a where() extension method using Func please have a look...

    public static IEnumerable Where ( this IEnumerable a , Func Method )
    {
    
        foreach ( var data in a )
        {
            //If the lambda Expression(delegate) returns "true" Then return the Data. (use 'yield' for deferred return)
            if ( Method.Invoke ( data ) )
            {
                yield return data;
            }
        }
    }
    

    You can use it like,

            foreach ( var item in Emps.Where ( e => e.Name == "Shiv" ).Select ( e1 => e1.Name ) )
            {
                Console.WriteLine ( item );
            }
    

提交回复
热议问题