C#: Anonymous method vs Named method

后端 未结 5 1429
渐次进展
渐次进展 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:36

    Explanation by Analogy

    Normally when we tell stories we refer to people by name:

    "Freddie"

    "Who's Freddie?"

    "You know, Freddie, Freddie from Sales - the male guy with the red hair, who burned the building down...?"

    In reality nobody cares who the person is, department he works etc. it's not like we'll refer to him every again. We want to be able to say: "Some guy burned down our building". All the other stuff (hair color, name etc.) is irrelevant and/or can be inferred.

    What does this have to do with c#?

    Typically in c# you would have to define a method if you want to use it: you must tell the compiler (typically):

    • what it is called,
    • and what goes into it (parameters + their types),
    • as well as what should come out (return type),
    • and whether it is something you can do in the privacy of your home or whether you can do it in public. (scope)

    When you do that with methods, you are basically using named methods. But writing them out: that's a lot of effort. Especially if all of that can be inferred and you're never going to use it again.

    That's basically where anonymous methods come in. It's like a disposable method - something quick and dirty - it reduces the amount you have to type in. That's basically the purpose of them.

    0 讨论(0)
  • 2020-12-10 05:45

    From MSDN:

    A delegate can be associated with a named method. When you instantiate a delegate by using a named method, the method is passed as a parameter. This is called using a named method. Delegates constructed with a named method can encapsulate either a static method or an instance method. Named methods are the only way to instantiate a delegate in earlier versions of C#. However, in a situation where creating a new method is unwanted overhead, C# enables you to instantiate a delegate and immediately specify a code block that the delegate will process when it is called. The block can contain either a lambda expression or an anonymous method.

    and

    In versions of C# before 2.0, the only way to declare a delegate was to use named methods. C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code. However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list. This means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions. For more information specifically about lambda expressions, see Lambda Expressions (C# Programming Guide). Creating anonymous methods is essentially a way to pass a code block as a delegate parameter. By using anonymous methods, you reduce the coding overhead in instantiating delegates because you do not have to create a separate method.

    So in answer to your question about when to use anonymous methods, then MSDN says: in a situation where creating a new method is unwanted overhead.

    In my experience it's more down to a question of code reuse and readability.

    Links:

    http://msdn.microsoft.com/en-us/library/98dc08ac.aspx

    http://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx

    Hope that helps

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-10 05:51

    Anonymous methods or anonymous functions, what seems to be the same, basically are delegates. As the link you point out: http://msdn.microsoft.com/en-us/library/bb882516.aspx describes, anonymous methods provide a simplified way to pass method to be executed by another method. Like a callback. Another way to see it, is think about lambda expressions.

    A named by the contrast is any common method.

    0 讨论(0)
  • 2020-12-10 06:00

    Let's start from a simple method.

    void MyMethod()
    {
      Console.WriteLine("Inside MyMethod"); //Write to output
    }
    

    The above method is a named-method which just writes Inside MyMethod to the output window.

    Anonymous methods are some methods used in some special scenarios (when using delegates) where the method definition is usually smaller where you don't specify the name of the method.

    For example, (delegate) => { Console.WriteLine("Inside Mymethod");}

    Just start writing some simple programs and in the due course, when you use delegates or some advanced concepts, you will yourself learn. :)

    0 讨论(0)
提交回复
热议问题