0

I need to make a program that shows a number from a double type array next to its corresponding element from a string type array.

Basically I have:

double[] number = new double[3]
number[0] = 1.1;
number[1] = 7.8;
number[2] = 6.0;

string[] text = new string[3]
text[0] = "Text1";
text[1] = "Text2";
text[2] = "Text3";

And I need some way to write something like text[1] - number[1]

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Something like: `Console.WriteLine($"{text[1]} - {number[1]}");`? If not, I'm not quite sure what you're asking... – Jon Skeet Apr 02 '21 at 20:15
  • 1
    `Console.Write(string.Join(Environment.NewLine, text.Zip(number, (t, n) => $"{t} - {n}")));` – Dmitry Bychenko Apr 02 '21 at 20:20
  • Does this answer your question? [Correlation of two arrays in C#](https://stackoverflow.com/questions/17447817/correlation-of-two-arrays-in-c-sharp) – Heretic Monkey Apr 02 '21 at 20:37

1 Answers1

0

You can do something like this, using each array's enumerator

var numberEnumerator = number.GetEnumerator();
var textEnumerator = text.GetEnumerator();
while (numberEnumerator.MoveNext() && textEnumerator.MoveNext())
    Console.WriteLine($"{textEnumerator.Current} - {numberEnumerator.Current}");

This is the basics. You can get fancier by using extension methods, generics, or pulling in a NuGet package such as MoreLinq, which has a Zip() method to combine sequences element by element.

Kit
  • 20,354
  • 4
  • 60
  • 103