How to build a list of elements in C#?

Posted on 2007-10-02
Last Modified: 2008-01-09
Hello experts,

  I am working with visual studio C# 2005 so I have access to the .NET framework.
I need a data structure (probably a List, but I am opened to any suggestions) in which there will be many elements (most of the time between 1 and 10). It is not necessarily ordered.

  The elements have many fields. By instance it could be:

  int sequenceNb;
  string toto = new string('firstElement');

 I need to find an element in the List by using ONLY its sequenceNb NOT its string value.
I will need to remove an element no matter of its location in the List.

  Which class so you suggest (List, Collection, ...)? Can you show me a code snippet?
It is not for an homework.


Question by:pascal_lalonde
    LVL 12

    Expert Comment

    A very simple example to store an element in an arraylist and retrieving it using the Find method:

    public partial class Form1 : Form
            System.Collections.ArrayList a = new System.Collections.ArrayList();
            public Form1()

                a.Add(new MyElementClass(1234, "Test"));

                MyElementClass m = Find(1234);

            public MyElementClass Find(int seqNumber)
                foreach (MyElementClass m in a)
                    if (m.SequenceNumber == 1234)
                        return m;

                return null;

        public class MyElementClass
            int sequenceNb;
            string toto;

            public MyElementClass(int sqNumber, string name)
                sequenceNb = sqNumber;
                toto = name;

            public int SequenceNumber
                    return sequenceNb;
    LVL 10

    Expert Comment

    if you want only to insert MyElementClass type objects use
    List<MyElementClass> myArray = new List<MyElementClass>();

    it is slightly faster that ArrayList since there is no boxing-unboxing
    LVL 2

    Accepted Solution

    If the sequenceNb is unique you could use a dictionary:

    1. Declare
    Dictionary<int, MyElementClass> elementLookup = new Dictionary<int, MyElementClass>();

    2. Add elements
    elementLookup.Add(sequenceNbValue, elementInstance);

    3. Retrieve elements by sequenceNb value
    MyElementClass retrievedElement = elementLookup[sequenceNbValue];

    Author Comment

    Wow! I like when I don't have to reinvent the wheel. Thank you!

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    754 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

    24 Experts available now in Live!

    Get 1:1 Help Now