Solved

Add an items from a list into like an array or collection

Posted on 2012-04-04
6
257 Views
Last Modified: 2012-04-05
Hi all,

This is a relatively simple quesiton I would expect. But in C#, if I have a list of items (names of items). I want to load them into a collection or something. Waht is the best way to do that?

An array, a collection or something...

I want to do something like:

FieldList.Add(strValue).

Then I will end up seeing if a value doesn't exist in that list.

So, I the list contains some fruits:

Apple
Orange
Grape
Banana

I want those to go into a list.

then I will eventually want to use like:

"Nut" and see if it exists in that list.

What is the best way to do that?

Thanks
0
Comment
Question by:davism
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 37807656
List<String> items = new List<String>();

items.Add(yourStringValue);

if(items.Exists(yourStringValue)
{
  //do something

}
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 37807663
In C#, you can use the generic List:

List<string> fruits = new List<string>();
fruits.Add( "Apple" );
fruits.Add( "Orange" );
etc.

if ( !fruits.Contains( "Apple" ) )
{
   // does not have "Apple" in the list.
}

You can also convert it to an array simply:


string[] fruitArray = fruits.ToArray();
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 37808010
A HashSet would perform better with lookups.  The Contains implementation on a List relies on sequential comparisons, while the HashSet implementation is based on a hash lookup.

var fruits = new HashSet<string>() { "Apple", "Orange", "Grape", "Banana" };

if (fruits.Contains("Nuts"))
{
	Console.WriteLine("We have Nuts");
}
else
{
	Console.WriteLine("We don't have Nuts let's buy some");
	
	fruits.Add("Nuts");
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:davism
ID: 37808370
Ok, I had that a little wrong.

I need the table to have like

Fruit
    Apples
    Oranges
    Bananas

Meat
    Steak
    Hamburget
    Chicken

So, that's like a diffent type of collection is it not? Does anybody have an example on how to implement something like that?

Thanks
0
 
LVL 22

Accepted Solution

by:
p_davis earned 100 total points
ID: 37810678
Dictionary<string, List<string>> items = new Dictionary<string, List<string>>();

if(!items.ContainsKey("Fruit"))
             items.Add("Fruit", new List<String>());

items["Fruit"].Add("Apples");  //... etc.
0
 
LVL 1

Author Closing Comment

by:davism
ID: 37813737
Thanks for the information and greatly appreciated!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question