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


Can anyone explain that is the right reason for using the action < T & gt; and Predicate & lt; T & gt; as representatives in C #

Are you asking why they exist? , Or why are they defined as' delegates?

Why they exist, perhaps the best reason is that if you want a representative who does not give any value and takes a single parameter of any type, you can define it yourself:

  Public representative zero MyDelegate (int size);  

and then create one later:

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

And, of course, you have to do this for each different type, which you want such a method.

Or, you can only take action & lt; T & gt; :

  Action & lt; Int & gt; Proc = New Action & lt; Int & gt; ((S) => (* / * accessories here);  

Of course, you can reduce it:

  action & lt; Int & gt; Proc = (s) = & gt; {/ * Works here} As  

"Why are they representative?" Because this is how the work reference manipulation is done in the NAT. We use delegates. Note the similarities in the above examples, that is, MyDailyGet is conceptually an verb & lt; Int & gt; . They are not the same thing as absolutely , because they have different types, but you have operations in a program with MyDelegate & lt; Int & gt; , and the program will work.


Comments