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 6 46096
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
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
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
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
If you want Contains, then ICollection<Tmay be the interface you want -
which Dictionary<TKey,TValue>.KeyCollection happens to implement...
Marc
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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>....
|
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:
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 <...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| | |