C#: Anonymous method vs Named method

痞子三分冷 提交于 2019-12-28 15:16:10

问题


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 methods

  1. A Method is a block of statements, which serves for code reusability & it also supports overloading with different SIGNATURE....for ex: drawShape(2pts), drawShape(3pts) etc...

  2. An Anonymous method is one with block of statements, but no name....(as its premature to ask, in wt situation we come across anonymous method...any articles, samples ...)

  3. Named method: Here's a link but at the end i didn't get what Named Method actually is...

Can anyone explain what a "Named" method is, and where do we use anonymous method?


回答1:


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



回答2:


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. :)




回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/20869312/c-anonymous-method-vs-named-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!