Reputation:
i have:
int[] numbers = { 1, 2, 3};
string[] words = { "one", "two", "three" };
and need the output to be
1=one
2=two
3=three
thanks
Upvotes: 2
Views: 134
Reputation: 7342
I see that everybody is jumping to Linq or IEnumerable extensions when you don't grasp the basics. It will be like putting a child to college so I suggest you first learn using loops, like the for loop.
for (int i = 0; i < numbers.Length; i++) {
Console.WriteLine(String.Format("{0}-{1}", numbers[i], words[i]));
}
And Math class basics
int total = Math.Min(numbers.Length, word.Length);
for (int i = 0; i < total; i++) {
Upvotes: 6
Reputation: 31464
LINQ example:
var query = numbers.Select((n, i) => string.Format("{0}={1}", n, words[i]));
Edit:
In .NET 4.0 you can use Zip
(as posted by mBotros) - there's even almost same example as what you're asking for on Zip MSDN doc page.
Upvotes: 3
Reputation: 1752
if they are the same size you can use
int[] numbers = { 1, 2, 3};
string[] words = { "one", "two", "three" };
var list = numbers.Zip (words, (n, w) => n + "=" + w);
but note if they differ in size the non match items will be ignored
Upvotes: 7