Solved

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

Posted on 2012-04-04
6
253 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
Comment Utility
List<String> items = new List<String>();

items.Add(yourStringValue);

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

}
0
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:davism
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the information and greatly appreciated!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now