0

I want to use a 2-D array in C#, e.g:

string[,] a = new string[,]
{
    {"aunt", "AUNT_ID"},
    {"Sam", "AUNT_NAME"},
    {"clozapine", "OPTION"},
};

My requirement is that when I pass "aunt" to this array I want to get corresponding AUNT_ID from the 2-D array.

0

5 Answers 5

4

As others have said, a Dictionary<string, string> would be better - and you can use a collection initializer to create it simply:

Dictionary<string, string> dictionary = new Dictionary<string, string>
{
    {"ant", "AUNT_ID"},
    {"Sam", "AUNT_NAME"},
    {"clozapine", "OPTION"},
};

If you're confident that your key is in the dictionary, and you're happy for an exception to be thrown otherwise:

string value = dictionary[key];

or if it might not be:

string value;
if (dictionary.TryGetValue(key, out value))
{
    // Use value here
}
else
{
    // Key wasn't in dictionary
}

If you really need to use an array, if you can change it to a multidimensional array (string[][]), you can use:

// Will throw if there are no matches
var value = array.First(x => x[0] == key)[1];

Or again to be more circumspect:

var pair = array.FirstOrDefault(x => x[0] == key);
if (pair != null)
{
    string value = pair[1];
    // Use value here
}
else
{
    // Key wasn't in dictionary
}

LINQ unfortunately doesn't work quite as well on rectangular arrays. It probably wouldn't be too hard to write a method to allow it to be treated "somewhat" like an array of arrays, admittedly...

Sign up to request clarification or add additional context in comments.

Comments

2

Use Dictionary<string, string> for that:

Dictionary<string, string> arr = new Dictionary<string, string>();
arr.Add("ant", "AUNT_ID");
arr.Add("Sam", "AUNT_NAME");
arr.Add("clozapine", "OPTION");

string k = arr["ant"]; // "AUNT_ID"

1 Comment

Note that using a collection initializer makes this code look much more like the originally-posted code.
1

The best option for you is to use Dictionary, but if you still wants to use 2D array, you may try the following

    string[,] a = new string[,]
                    {
                        {"ant", "AUNT_ID"},
                        {"Sam", "AUNT_NAME"},
                        {"clozapine", "OPTION"},
                    };
    string search = "ant";
    string result = String.Empty;
    for (int i = 0; i < a.GetLength(0); i++) //loop until the row limit
    {
        if (a[i, 0] == search)
        {
            result = a[i, 1];
            break; //break the loop on find 
        }

    }
    Console.WriteLine(result); // this will display AUNT_ID

Comments

0

It looks rather like you want a dictionary:

Dictionary<string, string> a = new Dictionary<string, string>();
a.Add("ant", "AUNT_ID");
a.Add("Sam", "AUNT_NAME");
a.Add("clozapine", "OPTION");

string s = a["ant"]; // gets "AUNT_ID"

To check if a key exists in the dictionary:

if (a.ContainsKey("ant")) {
  ...
}

Or:

string s;
if (a.TryGetValue("ant", out s)) {
  ...
}

1 Comment

But in some case 'ant' is not in the Dictionary then, it should give me an exception
-2
for (i=0; i<3; i++){
    if (!String.Compare(a[i][0], string)){
        stored_string= a[i][1];
    }
}

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.