:: SHARE THIS POST ::
Checked and Unchecked operators in C# Step By Step
In this article we will understand use of or working with Checked and Unchecked operators in C# Step By Step using example ....Click To Know More
Remember Top 3 important points of out and ref in C#
3 important points to remember while working with out and ref in C# ....Click To Know More
Install SQL Server 2014 Step by Step with example
In this article learn to install SQL Server 2014 step by step using our example image screen shots without any errors ....Click To Know More
Type Safe in C#.NET step by step using example
Here in this article let's understand Type Safe in CSharp.NET step by step with an example ....Click To Know More
App Domain in C#.NET Step by Step using example
In this article let's understand about app domain in csharp(C#).NET step by step using simple example. ....Click To Know More
Delay signing in C# with example
In this article athesham sir talks about delay signing and discuss Delay signing definition in more detail ....Click To Know More
Tuples in c# example step by step
In this Part 1 of the article we learn about Tuples which is not new to programming languages but it was new to C# when introduced in C# 4.0. We will see ....Click To Know More
C# Anonymous Types vs Tuples with an example
This article is the continuation of previous part 1 in which we had earlier seen how tuples reduces the code by making it more tidy and readable ....Click To Know More
Anonymous Methods in C# with an example Step by Step
This article written by "Ahtesham Shaikh" in this article understand Anonymous Methods in C# with an example Step by Step ....Click To Know More
Anonymous methods are moreover related to Delegates. Anonymous methods are extension and improvement to delegates. They make the code easy and more readable. Anonymous methods increase performance of the application overall as and when used instead traditional delegate. With anonymous methods you can use delegate within same function.
In this article we will see how to write code of anonymous methods in C# and compare it with traditional delegate. Also we will practically see how performance increases with use anonymous methods over traditional delegates.
This article written by "Ahtesham Shaikh", Ahtesham Shaikh is C# and .NETtrainer, He not only writes for C# but also take trainings on C# for freshers and mid-level developers.
Below is simple sample code of delegate which is written in a normal way where "Add" method is written separately then delegate "PointerAddFun"is created. Thereafter pointer "pointadd" is then pointed to "Add" method. Finally the pointer is invoked and given values perform add operation then results are displayed on the console prompt.
As said earlier with the use of Anonymous Methods we can reduce the line of code by removing overhead method which does the add operation as done in this example and write inline block of code within the Main method to perform add operation. Using Anonymous Methods is more appropriate where we have simple and single line of method likewise we have in this example.
Now we have seen how to write both traditional written delegate and also seen how to code using Anonymous Methods.
It is the time to do comparison between both of the methods and check out which performs better.
In order to do comparison we will write two "For loop" codes, first loop code which will run for 10 times and under to first loop code there will be second loop code which will run 1000 times. Under second loop code which is running 1000 times will execute anonymous delegate code within it.
Below image shown are the full steps: -
for (int y = 0; y < 10; y++)
for (int x = 0; x < 1000; x++)
Stopwatch objstpwtch = new Stopwatch();
int z = pointadd.Invoke(3 ,3);
Once writing of code is completed then build the entire solution and click on ctrl + F5 on keyboard to execute and see the result.
On console prompt you will see the execution result time taken for each 10 loops and each loop executed 1000 times.
Since the results for anonymous methods are out now we will recode it. All steps are same as we have seen aboveonly need is to remove existing in line code delegate and create a separate Add method outside. Once the Add method is created tie it up with pointer delegate. Below is the code which should be written for pointing delegate to method
PointerAddFunpointAdd = Add;
Rebuilt the solution and run the code using ctrl + F5 key on keyboard
Result will be seen on the command prompt for normal delegate
We have taken results of both the methods and put it on the table in front of you for ease of comparison. From below table, result for anonymous method where we have inline delegate code shows less time to process the loop as compare to normal way of delegate where Add method is outside.
Anonymous method Delegate
Normal way Delegate
So it is better to use anonymous methods inline delegate code under the circumstances where we have small and simple methods instead of creating full overhead methods outside.
Secondly performance increases with use of anonymous methods as we have tested above and got the results.
This article is a small water droplet from the .NET knowledge of pond. There are many other important topics which are essential to become successful programmer here is the site which we would like to share questpond.com which is answer for almost all questions in C# and .NET programming.
Also below is a video from their latest C# 100 hrs series hope you will like it as a fresher: -
More C# Articles To Read Next:
Understand Monitor vs Mutex vs Semaphore vs SemaphoreSlim - Onlinebuff
Views - 15202 |Category - C#
Thread Pooling in C#.NET with real time example - Onlinebuff
Views - 14257 |Category - C#
Step by Step Partial Classes and Partial Methods in C#.NET with example
Views - 7869 |Category - C#
Understand threading and types of threading in c# using an example
Views - 13446 |Category - C#
Understand difference between Early Binding and Late Binding with an example in C#
Views - 22550 |Category - C#
All fields marked with * are mandatory