0

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.

2
  • 3
    Solution: make a single array of CustomObject. Or key-value pairs like a dictionary. Or Tuple<name,mark> Commented Nov 25, 2015 at 17:56
  • 1
    you can use Zip from linq. see here stackoverflow.com/questions/5122737/… Commented Nov 25, 2015 at 17:56

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.