Alsalam alikom wa ra7mat Allah wa barakatoh
Today, I was checking on of my friends' blogs Mohammed Hossam, when I found that post about List.Foreach function, then I read this article Did it With .NET...
So, what is this all about !!
I'll talk about two aspects,
1- The use of it..
2- The Performance Issue..
- The use of it :
Consider this program :
foreach (int i in arr)
{
Console.WriteLine(i);
}
This looks neat enough.. but mm, what about this :
arr.ForEach(delegate(int i){ Console.WriteLine(i); });
or what about this :
arr.ForEach(i => Console.WriteLine(i); );
or what about this :
arr.ForEach(Console.WriteLine);
Looks nice, huh ;) ?
List.ForEach accepts as a paramater, a delegate object that takes type .. u can do whatever u want inside this delegate, and it'll be applied on each element of the List.
About the second example, i =>, this is just a sample for Lambda expression...
The third looks really neat actually, I really liked it...
2- The Performance Issue :
After reading the second article above, I can say that ForEach function is really really fast, that u can say it's equivilant to the traditional for (without compiler optimization).. and slightly faster than traditional for if u used some optimization parameters...
Read for Performance report here : Did it With .NET
Good luck everybody..
Alsalam alikom wa ra7mat Allah wa barakatoh
Today, I was checking on of my friends' blogs Mohammed Hossam, when I found that post about List
So, what is this all about !!
I'll talk about two aspects,
1- The use of it..
2- The Performance Issue..
- The use of it :
Consider this program :
foreach (int i in arr)
{
Console.WriteLine(i);
}
This looks neat enough.. but mm, what about this :
arr.ForEach(delegate(int i){ Console.WriteLine(i); });
or what about this :
arr.ForEach(i => Console.WriteLine(i); );
or what about this :
arr.ForEach(Console.WriteLine);
Looks nice, huh ;) ?
List
About the second example, i =>, this is just a sample for Lambda expression...
The third looks really neat actually, I really liked it...
2- The Performance Issue :
After reading the second article above, I can say that ForEach function is really really fast, that u can say it's equivilant to the traditional for (without compiler optimization).. and slightly faster than traditional for if u used some optimization parameters...
Read for Performance report here : Did it With .NET
Good luck everybody..
Alsalam alikom wa ra7mat Allah wa barakatoh
Comments
Post a Comment