2

first time posting here. I'm looking to use a while loop to define several strings/vars. This code gives an idea of what I'm looking for...

    int i = 0;
    while (i < 4)
    {
    string cmd1 = "Command 1";
    var num1 = 1;
    i++
    }

I'm trying to figure out how to get a while loop to somehow output...

string cmd1 = "Command 1";
var num1 = 1;
string cmd2 = "Command 2";
var num2 = 2;
string cmd3 = "Command 3";
var num3 = 3;
string cmd4 = "Command 4";
var num4 = 4;

Thank you in advance for any help.

2 Answers 2

2

If you do not have any repeating values, you could use Dictionary in C#

var nums = new Dictionary<int, string>();

var i = 1;
while(i <= 4)
{
    nums.Add(i, $"Command {i}");
    i++;
}

foreach(var item in nums)
{
    Console.WriteLine($"{item.Key} - {item.Value}");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Also be noted that the console writing syntax works in c#6
1

One liner :)

Enumerable.Range(1, 4).ToList().ForEach(i => Console.WriteLine("Command " + i));

Make sure to add this in using section.

using System.Linq;
using System.Collections;

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.