Delegate implementing each method of JavaScript

Source: Internet
Author: User

There are many confusing keywords in C #, such as Delegate,func, action, and predicate. Func, action and predicate are essentially delegate, look at the delegate concept below.

1 Delegate Concept

Delegate is essentially a pointer to a function that points to a different function, as long as the signature of the function is consistent with the proxy.

2 Delegate Applications

In fact, Func, Action, predicate and so are all delegate, just special delegate only. The ingenious application of delegate can greatly simplify code and increase flexibility. Here is a piece of JavaScript code, JS often use the array of each method to iterate over the array and handle it, as follows:

1  var arr = ["One", "one", "one", "three", "four"];       2  $.each (arr, function () {       3     alert (this);       4  });     5//above the results of each output are: One,two,three,four

So how to define an array by delegate in C # each method, you can pass the method to achieve flexible logic processing, static Listex class has a static each method, defined as follows:

1 public static t[] each<t> (t[] source, func<t, t> function) 2 {3  4     t[] ret =new t[source. Length]; 5     int i = 0; 6     foreach (T item in Source) 7     {8         ret[i]=function (item); 9         i++;10     }11     return ret ; 12}

Then we can define a string array and define a delegate as a function parameter to pass in, calling the Listex.each method:

1 var arr =new string[]{"One", "one", "one", "three", "four"};     2 var newarr= listex.each<string> (Arr,delegate (String x) {3     x=x+ "_do"; 4     return x;5});

Of course, you can use expressions to simplify:

1 var newArr2 = listex.each<string> (NEWARR, (string x) + x = x + "_do");

We can also define a where method to filter the array:

1 public static ilist<t> find<t> (ilist<t> source, predicate<t> predicate) 2 {3     list<t > ret = new list<t> (); 4     foreach (T item in Source) 5     {6         if (predicate (item)) 7         {8             ret. ADD (item); 9         }10     }11     return ret;12}13 public static t[] where<t> (t[] source, predicate<t> predicate) 14 {15     ilist<t> List=source. Tolist<t> ();     ilist<t> retlist= find<t> (list, predicate);     Retlist.toarray return <T> (); 18}

The call is as follows:

1 var newArr3 = listex.where<string> (arr, x = x = = "both");
3 Differences Overview

Func is the proxy that must specify the return value;

Action is an agent with a return value of void;

predicate is a proxy with a return value of bool;

Delegate implementing each method of JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.