Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Anonymous method vs Named method

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?

like image 707
c-sharp user Avatar asked Jan 01 '14 13:01

c-sharp user


3 Answers

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
like image 77
Igor Ševo Avatar answered Sep 22 '22 19:09

Igor Ševo


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

like image 26
now he who must not be named. Avatar answered Sep 20 '22 19:09

now he who must not be named.


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.

like image 25
BenKoshy Avatar answered Sep 20 '22 19:09

BenKoshy