The code is simple and working perfectly, but I wanted tips on how to optimize it, what would be the best methods to use to allocate less memory, good practices, etc.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
List<int> numeros = new List<int>();
List<int> pares = new List<int>();
List<int> impares = new List<int>();
Random random = new Random();
for(int i = 0; i < 20; i++)
{
int n = random.Next(1, 99);
numeros.Add(n);
}
foreach (int item in numeros)
{
if (item % 2 == 0)
pares.Add(item);
else
impares.Add(item);
}
Console.WriteLine("Todos os números:");
int index = 0;
foreach (int item in numeros)
{
if (index == numeros.Count - 1)
Console.Write(item + "." + "\n");
else
Console.Write(item + ", ");
index++;
}
index = 0;
Console.WriteLine("\n" + "Números pares:");
foreach (int item in pares)
{
if (index == pares.Count - 1)
Console.Write(item + "." + "\n");
else
Console.Write(item + ", ");
index++;
}
Console.WriteLine("\n" + "Números ímpares:");
index = 0;
foreach (int item in impares)
{
if (index == impares.Count - 1)
Console.Write(item + "." + "\n");
else
Console.Write(item + ", ");
index++;
}
}
}