Solved

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

Posted on 2012-04-04
6
255 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL LINE CONTINUATION ISSUE 12 33
Getting error in connectionstring with Excel. 30 35
Create a WebAPI using a generic return parameter 9 25
What Does This C# Code Block Do? 5 33
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

823 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