Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
C# Using array.Where in Arraylist

I have sets of codes in C# with array and I need to split the array into smaller size array. I have no errors when declaring the array with :

List<int> array = new List<int>();

However, the code execution prompts out error at the array.Where when I declare as :

var array = new ArrayList();

Is there any way I can use array.Where in array list? Below is my code :

List<int> array = new List<int>();

for (int i = 0; i <=20; i++){
  if (unitIsPresent) 
  {
    array.add(1);
  } 
  else
  {
    array.add(0)
  }
}

devidedArray = array.Where((e, i) => i >= 5 && i < 10).ToArray();
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Array List is a non generic collection type so it's good to store items in array where you don't consider the items types. So for this reason you can't use Linq methods that are used for generics collections like Where. My recommendation is use a List and convert it to Array with the Linq method provided.this way is very fast.

over 4 years ago · Santiago Trujillo Report

0

If you really want to use an ArrayList (really? why?), you can use OfType<int>() to change the IEnumerable to an IEnumerable<int>.

var devidedArray = array.OfType<int>().Where((e, i) => i >= 5 && i < 10).ToArray();

Alternatively you could use Cast<int>():

var devidedArray = array.Cast<int>().Where((e, i) => i >= 5 && i < 10).ToArray();

The difference between the two is that OfType() will silently ignore objects that cannot be cast to int while Cast() will fail with an InvalidCastException.

over 4 years ago · Santiago Trujillo Report

0

https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=net-5.0

look microsoft doc.

We don't recommend that you use the ArrayList class for new development. Instead, we recommend that you use the generic List class. ...

just use List<T> to replace ArrayList.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!