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

后端 未结 8 2364
情话喂你
情话喂你 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

    Have a blog post on this. Using Func you can resolve some of functional discrepancy. Read here.

    0 讨论(0)
  • 2020-12-13 13:40

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

    public static IEnumerable<Tsource> Where<Tsource> ( this IEnumerable<Tsource> a , Func<Tsource , bool> 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 );
            }
    
    0 讨论(0)
提交回复
热议问题