SentineL
SentineL

Reputation: 4732

get an array of Dictionary's keys

I have a dictionary, where both keys and values are my own classes. How can I get an array of keys? How can I find out what keys are in this dictionary?

Upvotes: 1

Views: 2474

Answers (3)

Azhar Mansuri
Azhar Mansuri

Reputation: 705

You can use lamda expression for the same. Here is the sample code for it.

Dictionary<string, string> Test = new Dictionary<string, string>();
            Test.Add("Azhar","Mansuri");
            Test.Add("Azhar2", "Mansuri");
            Test.Add("Azhar3", "Mansuri");
            Test.Add("Azhar4", "Mansuri");
            Test.Add("Azhar5", "Mansuri");
            Test.Add("Azhar6", "Mansuri");

            string[] key = Test.Select(s => s.Key).ToArray();

key array will return all the keys of dictionary.

Update : since you want to know the key there is no better way than this:

for (var k:Object in dictionary) { var value:ValType=dictionary[k]; var key:KeyType=k; // do stuff }

It is just a sample code. However it may be helpful for you.

Upvotes: 1

Azzy Elvul
Azzy Elvul

Reputation: 1438

var dict = new Dictionary();

dict["a"] = 1;
dict["b"] = 2;
var arrResult: Array = new Array();

for ( var key: Object in dict )
{
    arrResult.push( key );
}

trace( arrResult ) -> b,a

Upvotes: 6

HDdeveloper
HDdeveloper

Reputation: 4404

look at the link for action script

Just use for ios

NSArray*keys=[dict allKeys];

In general, if you wonder if a specific class has a specific method, look up Apple's own documentation. In this case, see NSDictionary class reference. Go through all the methods. You'll discover many useful methods that way.

Upvotes: -2

Related Questions