Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Consider an array of strings:
string[] strs={"uno", "dos", "tres"};
Consider a method like:
static void shows(string s){ Console.WriteLine(s);}
Invoking that method for each string in the array, one way:
foreach(string s in strs) shows(s);
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) amethod(s);}
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.Add("one");strs.Add("two");strs.Add("three");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.