Solved

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

Posted on 2012-04-04
6
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

628 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