Code Library.... C# | Microsoft.Net

Predicate and Anonymous functions in . Net

During the development of one of my project I realized that using predicates are much faster that manual iterations in collections and array list. Just I was wondering why... because any way libraries too have to iterate by each object. But the truth is that when we use predicate or the Action delegate, the is a huge amount of gain both in execution time and performance

Basically a predicate in .Net (System.Predicate) is just a delegate to a user defined method which return true of false base upon some criteria, thus helps in decision making to the call of certain functions like List.FindAll, List.Find etc.

Where as Action delegate (System.Action) specifies a particular action or procedure which has to be applied on each element of the array List and obviously will not be just a Boolean condition determining function
E.g.:- List.ConvertAll for converting each element in an array or List to some another type.
List.Foreach etc….

While Explaining about this there is one more concept or in other words .Net compatible language compiler magic mainly called as anonymous functionsin .Net

This makes searching much easier and much cleaner and also some extensibility to the developer, so that he can implement his own decision while searching.
This function may look like
delegate (string msg) { return “ I am anonymous” }

As the function denotes the method is almost passed like inline and there is no name specified for this function.
That means we will not be able to recall this method in another place unless if we don’t have the delegate reference.
We used to directly assign this type functions to the required delegate reference
blog comments powered by Disqus