1

I'm trying to read values from a console application to add them to a two-dimensional array.

Current code:

public void getUserResponses(int amount)
{
    int current = 0;

    string[,] userArray = new string[amount, 2]; // Array

    while (current < amount)
    {
        Console.WriteLine("Please enter username #" + current);
        string username = Console.ReadLine();
        // Add username to array
        current++;
    }
}

As you can see in the code, I want to read the username and then add it to the array.

Example:

Read the username

string username = Console.ReadLine();

And then add the username to the array in the following format:

userArray[username][placeholder]

How could I manage this?

8
  • 1
    why do you have 2nd dimension in your array? Commented Jan 19, 2017 at 21:30
  • 2
    consider using a data structure and/or a dictionary. Commented Jan 19, 2017 at 21:30
  • Gonna add two values to the array, the second one isnt created yet but ill need it later on. Commented Jan 19, 2017 at 21:30
  • 1
    @user7347727 - if you need 2 values, consider using a List<Tuple<T1, T2>> instead of a 2-dimensional array. Commented Jan 19, 2017 at 21:31
  • @LeopardSkinPillBoxHat nice readable Item1 and Item2 Commented Jan 19, 2017 at 21:33

1 Answer 1

3

You can access a two dimensional array by doing array[i,j] = value;

From what your describing it feels like a Dictionary<string, string> might be more relevant. It will allow you to map strings with keys (the name in your case)

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.