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

  • SEARCH
  • Home
  • 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 7907951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:41:44+00:00 2026-06-03T11:41:44+00:00

Have been hearing all around ArrayList getting obsolete these days. But I do have

  • 0

Have been hearing all around ArrayList getting obsolete these days. But I do have a genuine case to use an ArrayList, like this:

    ArrayList GetArrayInt()
    {
        ArrayList l = new ArrayList();
        l.Add(2);

        return l;
    }

    ArrayList GetArrayBool()
    {
        ArrayList l = new ArrayList();
        l.Add(true);

        return l;
    }

    void Process(ArrayList l)
    {
        foreach (var o in l)
        {
            if (o is int)
            {

            }
            else
            {

            }
        }
    }

Then I call Process like this:

    private void Form1_Load(object sender, EventArgs e)
    {
        Process(GetArrayInt());
        //or
        Process(GetArrayBool());
    }

The bottom line is I dont need to write two Process methods separately for an ArrayList of ints and bools.

But I hear all negatives about ArrayLists. So I wanna implement it using List<T>. This is what I come up with:

    List<bool> GetListBool()
    {
        var l = new List<bool>();
        l.Add(true);

        return l;
    }

    List<int> GetListInt()
    {
        var l = new List<int>();
        l.Add(2);

        return l;
    }

    void Process(List<object> l)
    {
        foreach (var o in l)
        {
            if (o is int)
            {

            }
            else
            {

            }
        }
    }

Now how do I call it?

    private void Form1_Load(object sender, EventArgs e)
    {
        Process(GetListInt()); //error
        Process(GetListBool()); //error
        Process((List<object>)GetListBool()); //error
    }

The only solution here I found is to write separate Process methods for List<bool> and List<int> which is then two methods (my actual code is a lot more complex, so going by this approach I will need to write twenty such Process methods all which do the same thing).

How can I go about writing just one Process method in List<T> approach? Can List<T> replace an ArrayList even here?

Update: all the three answers work, but I liked @Phil’s answer the best. Thanks all

  • 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-06-03T11:41:47+00:00Added an answer on June 3, 2026 at 11:41 am

    As Mark Gravell says, what your doing is odd, but you could use type inference and use Process<T>(IEnumerable<T>) if you want to:

    void Main()
    {
        Process(GetInts());
        Process(GetBools());
    }
    
    IEnumerable<int> GetInts()
    {
        return new List<int>{1,2,3};
    }
    
    IEnumerable<bool> GetBools()
    {
        return new List<bool>{true, false, true};
    }
    
    void Process<T>(IEnumerable<T> items)
    {
        foreach (var item in items)
        {
            if(item is int)
            {
               Debug.WriteLine("int: " + item.ToString());
            }
            if(item is bool)
            {
               Debug.WriteLine("bool: " + item.ToString());
            }
        }
    }
    

    The more sensible way that doesn’t require if(item is int) etc would be to have an overload of Process for each type

    void Process(IEnumerable<int> items) { foreach(...) };
    void Process(IEnumerable<bool> items) { foreach(...) };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been hearing a lot of good things about DVCS systems, in particular
I have been hearing a lot about Ruby and possibly even Javascript being true
I have been hearing and reading about Agile for years. I own a book
I have been hearing that with unit testing we can catch most of the
I have been hearing this term quite a lot. I have a bunch of
I have been hearing a lot of hype about MVVM for WPF. When do
I've been hearing about triggers, and I have a few questions. What are triggers?
have been playing with Cubepoints (wordpress plugin) and have installed the ranks module but
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been hearing a lot about Project Euler so I thought I solve

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.