Suppose there are two arrays. One array is used to store names and the other array to store marks. How can I display a name with its corresponding mark? Both arrays are of different types.
Asked
Active
Viewed 107 times
0
-
3Solution: make a single array of `CustomObject`. Or key-value pairs like a dictionary. Or `Tuple
` – ryanyuyu Nov 25 '15 at 17:56 -
1you can use Zip from linq. see here http://stackoverflow.com/questions/5122737/what-is-the-use-of-enumerable-zip-extension-method-in-linq – M.kazem Akhgary Nov 25 '15 at 17:56
1 Answers
1
Try to use something like:
int[] marks = { 1, 2 };
string[] names = { "one", "two"};
var dictionary = names.Zip(marks, (s, i) => new { s, i })
.ToDictionary(item => item.s, item => item.i);
or
var dictionary = new Dictionary<string, int>();
for (int index = 0; index < marks.Length; index++)
{
dictionary.Add(names[index], marks[index]);
}
and then
foreach (var item in dictionary )
{
Console.WriteLine("{0}, {1}",
item.Key,
item.Value);
}

Roman Marusyk
- 23,328
- 24
- 73
- 116