Reputation: 143
I can't understand why delegate is used here:
List<string> temp_list = new List<string>();
string[] temp_array;
temp_array = Array.ConvertAll(arr_DL[m], delegate (int j) { return j.ToString(); });
temp_list.AddRange(temp_array.ToList());
the code is supposed to convert the array to list. Can someone help explain the use of delegate here?
Upvotes: 1
Views: 131
Reputation: 24957
This part:
delegate (int j) { return j.ToString(); }
creates so-called "anonymous method" as second parameter TOutput
, which used to convert int variable input into String then passes it into Array.ConvertAll()
method. Anonymous methods used widely on C# 2.0, where in C# 3.0 they substituted with lambda expression like this:
delegate String output (int j);
String output = x => { x.ToString(); }
Reference: https://msdn.microsoft.com/en-us/library/bb882516.aspx
CMIIW.
Upvotes: 1
Reputation: 2300
That because it's the second parameter of ConvertAll method. It represent how each element in initial Array
should be converted:
A Converter<TInput, TOutput> that converts each element from one type to another type.
So your delegate sais "convert each int
j
to string
by performing j.ToString();
"
Upvotes: 1