Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda