C#: Anonymous method vs Named method

后端 未结 5 1434
渐次进展
渐次进展 2020-12-10 05:19

I\'m new to SO and programming and learning day by day with bits and pieces of tech (C#) jargons.

After Googling for a while, below is what I\'ve researched about

5条回答
  •  無奈伤痛
    2020-12-10 05:51

    A named method is a method you can call by its name (e.g. it is a function that has a name). For example, you have defined a function to add two numbers:

    int f(int x, int y)
    {
        return x+y;
    }
    

    You would call this method by its name like so: f(1, 2);.

    Anonymous method is a method that is passed as an argument to a function without the need for its name. These methods can be constructed at runtime or evaluated from a lambda expression at compile time.

    These methods are often used in LINQ queries, for example:

    int maxSmallerThan10 = array.Where(x => x < 10).Max();
    

    The expression x => x < 10 is called a lambda expression and its result is an anonymous function that will be run by the method Where.

    If you are a beginner, I would suggest you first read about more basic stuff. Check out the following links:

    • http://www.completecsharptutorial.com/
    • http://www.csharp-station.com/tutorial.aspx
    • http://www.homeandlearn.co.uk/csharp/csharp.html

提交回复
热议问题