Why are Action <T> and Predicate <T> used or defined as delegates?

Can someone explain what is the exact reason for using Action<T> and Predicate<T> as delegates in C #

+4
source share
5 answers

You ask why they exist, or why they are identified as delegates?

As to why they exist, perhaps the best reason is convenience. If you want the delegate to not return values ​​and accept a single parameter of some type, you could define it yourself:

 public delegate void MyDelegate(int size); 

And then create one:

 MyDelegate proc = new MyDelegate((s) => { // do stuff here }); 

And, of course, you will have to do this for every other type that you want to have in this way.

Or you can just use Action<T> :

 Action<int> proc = new Action<int>((s) => { /* do stuff here */ }); 

Of course you can shorten this:

 Action<int> proc = (s) => { /* do stuff here */ }); 

As for "why are they delegates?" Due to the way that function references are handled in .NET: we use delegates. Note the similarities in the examples above. That is, MyDelegate conceptually the same as an Action<int> . They are not quite the same as they are of different types, but you can easily replace every instance of this MyDelegate in the program with an Action<int> , and the program will work.

+11
source

What else will they be? Delegates are the most flexible and closest to the fact that Action<T> , Predicate<T> and Func<T> are modlling - a list of calls with the specified types of parameters (aka delegates).

+4
source

The best way to explain why an idiom was chosen to perform predicate actions and operations using the Action<T> and Predicate<T> delegates is to consider an alternative first. How could you do the same without delegates? The following would be best to have IAction and IPredicate and force developers to declare a class and implement an appropriate interface for each type of operation they need. But the problem with this approach is that it requires more effort, more distribution of the class, and less repaired code. The delegate approach is much more convenient, because you can write a logical line with where it will be used using anonymous methods or lambda expressions.

+3
source

Because they are delegates.

+2
source

You view the problem from the wrong point of view.

The question should not be "Why are delegates Action<> , Func<> and Predicate<> ?" (The answer is "because they should be")

The real question is: β€œWhy do we have certain named objects to handle simple delegation tasks? Why use Action<> in the place where the regular delegate will work?”

The answer is that these specific delegates are used a lot in the CLR environment itself, and since then they have been used in CLR methods, where end developers will have to define the method to call, the declaration for the delegate should be public. Thus, Microsoft could identify thousands of delegates, each of which is used by only one CLR method, or define an easy way to determine which type of delegate is needed.

+1
source

All Articles