Anonymous types use the keyword var. Var is also used in Pascal and Delphi today, but var in Delphi is like ByRef in Visual Basic (VB) or ref in C#. The var introduced with .NET 3.5 indicates an anonymous type. Now, our VB friends are going to think, “Well, we have had variants for years in VB.” But var is not a dumbing down and clogging up of C#. Anonymous types are something new and necessary.
1. A
Understanding Generic Anonymous Methods and Lambda Expressions in C#
Posted Date: 1. May 2014 Author: Anil Sharma
Categories: .Net Framework, LINQ, C Sharp
Keywords: Generic Anonymous Methods, Anonymous Methods, Lambda Expressions, Lambda Expressions in C#, Lambda
Expressions Tutorials
nonymous methods behave like regular methods except that they are unnamed. Anonymous Methods were introduced
as an alternative to defining delegates that did very simple tasks, where full-blown methods amounted to more than
just extra typing. Anonymous methods also evolved further into Lambda Expressions, which are even shorter methods.
What is Anonymous Methods?
An anonymous method is like a regular method but uses the delegate keyword, and doesn’t require a name, parameters, or
return type. Following code example shows a regular method (used as a delegate for the CancelKeyPress event, Ctrl+C in a
console application) and an anonymous delegate that performs the same role.
The regular method which is uses as delegate is named ConsoleCancelEventHandler. The second statement that begins with
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Program
{
static void Main(string[] args)
{
// ctrl+c
Console.CancelKeyPress += new ConsoleCancelEventHandler
(Console_CancelKeyPress);
// anonymous cancel delegate
Console.CancelKeyPress +=
delegate
{
Console.WriteLine(“Anonymous Cancel pressed”);
};
Console.ReadLine();
}
static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Console.WriteLine(“Cancel pressed”);
}
}
http://www.dotnet-stuff.com/tutorials/c-sharp/understanding-generic-an...
1 of 4 8/18/2014 10:22 AM
2. the Console.CancelKeyPress += delegate demonstrates an anonymous method (delegate) that is equivalent to the longer
form of the method. The point of notice is that because the parameters in the delegate aren’t used, they are omitted from the
anonymous delegate. You have the option of using the parameter types and names if they are needed in the delegate.
Understanding Anonymous Generic Methods:
Delegates are really just methods as they are mostly used as event handlers. Generic methods are those that have
parameterized types. (You can think about replaceable data types.) Therefore, anonymous generic delegates are anonymous
methods that are associated with replaceable parameterized types. A very useful type is Func<t>. This generic delegate is
defined in the System namespace and can be assigned to delegates and anonymous delegates with varying return types and
parameters, which makes it a very flexible and useful.
Let’s take an example to understand that how we can use Anonymous Generic methods, following are simple example for
Anonymous Generic Methods
For all intents and purposes, Factorial is a nested function. above code used Func<long,long>, where the first long parameter
represents the return type and the second is the parameter. Notice that the listing also used a named parameter for the
anonymous delegate.
Understanding Lambda Expressions in C#
A Lambda Expression is a concise delegate. The left side (of the =>) represents the arguments to the function and the right
side is the function body. In below listed example, the Lambda Expression is assigned to the delegate FunctionPointer, but
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Program
{
static void Main(string[] args)
{
System.Func<long, long> Factorial =
delegate(long n)
{
if(n==1) return 1;
long result=1;
for(int i=2; i<=n; i++)
result *= i;
return result;
};
Console.WriteLine(Factorial(6));
Console.ReadLine();
}
}
http://www.dotnet-stuff.com/tutorials/c-sharp/understanding-generic-an...
2 of 4 8/18/2014 10:22 AM
3. .NET 3.5 defines anonymous delegates like Action<t> and Func<t>, which can be used instead of the more formal, longer
delegate statement. This means that you don’t have to define the delegate statement in below example.
Following is an example shows the revision of above example, replacing the delegate statement with the generic Action<t>
delegate type.
Lambda Expressions we can define automatic properties. The basic idea behind automatic properties is that many times,
programmers define properties that are just wrappers for fields and nothing else happens. If there are no additional behaviors,
automatic properties allow the compiler to add the field, getter, and setter.
Summary: Anonymous Methods and Lambda Expressions are very powerful and useful in c#. There are predefined Generic
Delegates Func<t>, Predicate<t> and Action<t>, are very useful for programmers. We will understand these into a separate
article. Anonymous Methods and Lambda Expressions are widely used with Linq. You can begin exploring how Lambda
Expressions support LINQ queries by seeing how Lambda Expressions are used in extension methods such as Select<t>,
Where<t>, or OrderBy<t>.
Keen to here from you...!
1
2
3
4
5
6
7
8
9
10
11
class Program
{
delegate void FunctionPointer(string str);
static void Main(string[] args)
{
FunctionPointer fp =
s => Console.WriteLine(s);
fp("Dot Net Stuff!");
Console.ReadLine();
}
}
1
2
3
4
5
6
7
8
9
10
class Program
{
static void Main(string[] args)
{
System.Action<string> fp =
s => Console.WriteLine(s);
fp("Dot Net Stuff!");
Console.ReadLine();
}
}
http://www.dotnet-stuff.com/tutorials/c-sharp/understanding-generic-an...
3 of 4 8/18/2014 10:22 AM
4. If you have any questions related to what's mentioned in the article or need help with any issue, ask it, I would love to here from
you. Please MakeUseOf Contact and i will be more than happy to help.
About the author
Anil Sharma is Chief Editor of dotnet-stuff.com. He's a software professional and
loves to work with Microsoft .Net. He's usually writes articles about .Net related
technologies and here to shares his experiences, personal notes, Tutorials,
Examples, Problems & Solutions, Code Snippets, Reference Manual and
Resources with C#, Asp.Net, Linq , Ajax, MVC, Entity Framework, WCF, SQL
Server, jQuery, Visual Studio and much more...!!!
Connect with the author: • Google • Linkedin
http://www.dotnet-stuff.com/tutorials/c-sharp/understanding-generic-an...
4 of 4 8/18/2014 10:22 AM