We help IT Professionals succeed at work.

How to build a list of elements in C#?

pascal_lalonde
on
1,695 Views
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:

MyElementClass
{
  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.

Regards,

Pascal
Comment
Watch Question

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()
        {
            InitializeComponent();

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

            MyElementClass m = Find(1234);
            MessageBox.Show(m.SequenceNumber.ToString());
        }

        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
        {
            get
            {
                return sequenceNb;
            }
        }
    }

Commented:
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
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.