473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert Dictionary<string,SomeType> keys to List<string>


Many times I have a Dictionary<string, SomeTypeand need to get the
list of keys out of it as a List<string>, to pass to a another method
that expects a List<string>.

I often do the following:

<BEGIN CODE>
List<stringkeyNameList = new List<string>();

foreach (string keyName in this.myDictionary.Keys)
{
keyNameList.Add(keyName);
}

someObject.someMethod(keyNameList);
<END CODE>

But it seems like there should be something built-in to give me a List
of the keys.
Am I missing something, or am I doing it the best way?

Thanks
Buzz

Aug 8 '06 #1
6 46153
Buzz,

You will want to do this:

// Get the list.
List<stringkeyNameList = new List<string>(this.myDictionary.Keys);

That will populate the list for you with the keys in the dictionary.

If your method is not going to modify the list, then you are better off
taking a parameter of IEnumerable<stringand not List<string>. A list is
desirable when you have to make modifications to the list, but if you are
just iterating through the list, without having to make changes to the list
(but maybe to the members of the list), then IEnumerable<stringis the
better choice.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<bu*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>
Many times I have a Dictionary<string, SomeTypeand need to get the
list of keys out of it as a List<string>, to pass to a another method
that expects a List<string>.

I often do the following:

<BEGIN CODE>
List<stringkeyNameList = new List<string>();

foreach (string keyName in this.myDictionary.Keys)
{
keyNameList.Add(keyName);
}

someObject.someMethod(keyNameList);
<END CODE>

But it seems like there should be something built-in to give me a List
of the keys.
Am I missing something, or am I doing it the best way?

Thanks
Buzz

Aug 8 '06 #2
Thank you Nicholas.
Great advice. I'll be using your IEnumerable tip also.

Buzz

Nicholas Paldino [.NET/C# MVP] wrote:
Buzz,

You will want to do this:

// Get the list.
List<stringkeyNameList = new List<string>(this.myDictionary.Keys);

That will populate the list for you with the keys in the dictionary.

If your method is not going to modify the list, then you are better off
taking a parameter of IEnumerable<stringand not List<string>. A list is
desirable when you have to make modifications to the list, but if you are
just iterating through the list, without having to make changes to the list
(but maybe to the members of the list), then IEnumerable<stringis the
better choice.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<bu*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

Many times I have a Dictionary<string, SomeTypeand need to get the
list of keys out of it as a List<string>, to pass to a another method
that expects a List<string>.

I often do the following:

<BEGIN CODE>
List<stringkeyNameList = new List<string>();

foreach (string keyName in this.myDictionary.Keys)
{
keyNameList.Add(keyName);
}

someObject.someMethod(keyNameList);
<END CODE>

But it seems like there should be something built-in to give me a List
of the keys.
Am I missing something, or am I doing it the best way?

Thanks
Buzz
Aug 9 '06 #3
Nicholas Paldino [.NET/C# MVP] wrote:
Buzz,

You will want to do this:

// Get the list.
List<stringkeyNameList = new List<string>(this.myDictionary.Keys);

That will populate the list for you with the keys in the dictionary.

If your method is not going to modify the list, then you are better off
taking a parameter of IEnumerable<stringand not List<string>. A list is
desirable when you have to make modifications to the list, but if you are
just iterating through the list, without having to make changes to the list
(but maybe to the members of the list), then IEnumerable<stringis the
better choice.
If you just want an IEnumerable<K>, then Dictionary<K,V>.Keys is a
Dictionary.KeyCollection<K>, which implements IEnumerable<Kalready.

static void Main(string[] args)
{

Dictionary<string, intdict = new Dictionary<string, int>();

dict.Add("foo", 1);
dict.Add("bar", 2);
dict.Add("pop", 3);

ListStrings(dict.Keys);

Console.ReadLine();
}

static void ListStrings(IEnumerable<stringlist)
{
foreach (string s in list)
{
Console.WriteLine(s);
}
}
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 9 '06 #4
I was passing List<stringbut now I've changed to IEnumerable<string>.
But now I hit a new ugliness...
I used to take that List and use the Contains method. Now with
IEnumerable I don;t have Contains. So it looks like I'll have to
foreach through... which seems kind of lame.

So it seems I DO want to pass a List<stringand just create the list
in the more elegant way that Nicholas suggsted in the first place:
// Get the list.
List<stringkeyNameList = new List<string>(this.myDictionary.Keys);
Unless there is yet more that I am missing.
Buzz

Larry Lard wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
Buzz,

You will want to do this:

// Get the list.
List<stringkeyNameList = new List<string>(this.myDictionary.Keys);

That will populate the list for you with the keys in the dictionary.

If your method is not going to modify the list, then you are better off
taking a parameter of IEnumerable<stringand not List<string>. A list is
desirable when you have to make modifications to the list, but if you are
just iterating through the list, without having to make changes to the list
(but maybe to the members of the list), then IEnumerable<stringis the
better choice.

If you just want an IEnumerable<K>, then Dictionary<K,V>.Keys is a
Dictionary.KeyCollection<K>, which implements IEnumerable<Kalready.

static void Main(string[] args)
{

Dictionary<string, intdict = new Dictionary<string, int>();

dict.Add("foo", 1);
dict.Add("bar", 2);
dict.Add("pop", 3);

ListStrings(dict.Keys);

Console.ReadLine();
}

static void ListStrings(IEnumerable<stringlist)
{
foreach (string s in list)
{
Console.WriteLine(s);
}
}
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 9 '06 #5
If you want Contains, then ICollection<Tmay be the interface you want -
which Dictionary<TKey,TValue>.KeyCollection happens to implement...

Marc
Aug 9 '06 #6
Marc Gravell wrote:
If you want Contains, then ICollection<Tmay be the interface you want -
which Dictionary<TKey,TValue>.KeyCollection happens to implement...
And if *all* we want to do is to see whether there is an entry with a
particular TKey, we just have to use Dictionary<,>.ContainsKey!

I love System.Collections.Generic!

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
52906
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
4
23918
by: Mark Rae | last post by:
Hi, Is it possible to create a case-insensitive List<stringcollection? E.g. List<stringMyList = new List<string>; MyList.Add("MyString"); So that:
7
57512
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
5
3822
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer...
44
39122
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
2
5849
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
5
3992
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a list of type object. The object has an ID as one of the elements and I would like to create another list that just has objects with unique IDs. For example in the list if I have...
5
2320
by: hanna88 | last post by:
hello there, i would like to convert a list of vector int into a list of string where i need to add some space between those char in that string. what's on my mind is that to use sstream to...
1
4381
by: hanna88 | last post by:
what's the best way to convert a list of string to a vector<int>? what i have now is for loop and iterate each char in the string string base=""; //i have vector<int>row,vector <...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.