CLR v2.0 Array.ForEach and List.ForEach

Consider an array of strings:
string[] strs={"uno", "dos", "tres"};

Consider a method like:
static void shows(string s)

Invoking that method for each string in the array, one way:
foreach(string s in strs)

There is need to invoke different methods other than 'shows' for the entire array, then consider:
delegate void process_string(string s);
void process_array(string[] strs, process_string amethod)
  foreach(string s in strs)

Used this way:
string[] strs={"uno", "dos", "tres"};
process_array(strs,new process_string(shows));

The above is good CLR v1.1 code, with CLR v2.0 consider:
string[] strs={"uno", "dos", "tres"};
Array.ForEach<string>(strs,new Action<string>(shows));

Ok with arrays, but also with generic List collection:
System.Collections.Generic.List<string> strs=new System.Collections.Generic.List<string>();
strs.ForEach(new Action<string>(shows));

If shows is not invoked from any other place, consider:
strs.ForEach(new Action<string>(delegate(string s) { w.WriteLine(s); }));

One method (process_array) or two (shows) and one delegate (process_string) saved.

Comments (0)

Skip to main content