Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 984495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:01:32+00:00 2026-05-16T05:01:32+00:00

I’m working with ArrayList in C# and I am wondering how I can add

  • 0

I’m working with ArrayList in C# and I am wondering how I can add objects to an ArrayList and then retrieve the values from it?

In short, how can I add, delete, edit and read from an ArrayList containing objects of classes?

Thankful for all help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:01:33+00:00Added an answer on May 16, 2026 at 5:01 am

    Unless you are in a situation where you must use .NET 1.0/1.1, or need to interact with legacy code that uses ArrayList – you should really avoid using ArrayLists in new code. Use the generic collection type List<> instead.

    The operations to add, remove, and replace an item in List<T> are quite straightforward.

    Let’s say you have some hypothetical type Animal, instances of which you will store in a list:

    Animal dog = new Animal("dog");
    Animal cat = new Animal("cat");
    
    List<Animal> animalList = new List<Animal>();
    
    // example of adding items to the list
    animalList.Add( dog );
    animalList.Add( cat );
    
    // example of removing items form the list
    animalList.Remove( cat );
    
    // example of replacing an item at a given position
    animalList[0] = new Animal("giraffe");
    

    The public interfaces for List<T> and ArrayList are actually quite similar. The main difference, is that ArrayList can only store object references since it was implemented before .NET supported generics.

    ArrayList listOfObjects = new ArrayList();
    int myAge = 207;
    listOfObjects.Add( (object)myAge );
    

    In the example above, you MUST cast types like int (which are value types in .NET) to object. This results in a boxing conversion – which copies the int value type to a new location on the heap, and passes it to ArrayList. Boxing conversions, are one of the disadvantages of using ArrayList – List<T> avoids this by virtue of being a generic class. Another issue is that ArrayList does not prevent you from mixing different types in the list together. For instance:

    listOfObjects.Add( (object)myAge );
    listOfObjects.Add( "Hello World" );
    

    are both allowed. However, when accessing elements of an ArrayList, you must know what type you are trying to retrieve. This makes ArrayList more fragile as a collection type, because the caller must write code to either protect themselves from arbitrary types being stored in the ArrayList, or else use reflection and runtime type checks to convert the values being stored. List<T> avoids both of these problems by allowing the compiler to help verify that only appropriate types are stored in the collection (those that match the type parameter T in List<T>).

    There’s a great deal more that could be written about interacting with collections – and in fact there is. Here’s a link to just one of many great books on the subject. My advice would be, before you begin writing code in .NET/C#, you should take the time to familiarize yourself with the basic concepts of the C# language and type system – what are reference vs. value types. What are primitives. What are generics. etc. This will help ensure that when you start writing code, the code does what you need it to do. C# has a sophisticated and rich type system- as well as a vast library of framework classes. It’s important to have a good grounding in the core aspects of the language before you get too deep into writing actual code. Examples like those I show above will only get you so far – and they already introduce numerous language concepts: variables, constructors, generics, boxing conversions, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.