CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
wrongMethod();
rightMethod();
anotherMethod();
}
private static void wrongMethod()
{
var inputList = new List<int> { 15, 24, 11, 22, 10, 12 };
for (int i = 0; i < inputList.Count; i++)
{
if (inputList[i] % 11 == 0)
{
inputList.RemoveAt(i);
}
}
foreach (var element in inputList)
{
Console.Write(element + " ");
}
Console.WriteLine();
}
private static void rightMethod()
{
var filteredList = new List<int>();
var inputList = new List<int> { 15, 24, 11, 22, 10, 12 };
for (int i = 0; i < inputList.Count; i++)
{
if (inputList[i] % 11 != 0)
{
filteredList.Add(inputList[i]);
}
}
foreach (var element in filteredList)
{
Console.Write(element + " ");
}
Console.WriteLine();
}
private static void anotherMethod()
{
var inputList = new List<int> { 15, 24, 11, 22, 10, 12 };
foreach (var element in inputList.Where(x => x % 11 != 0))
{
Console.Write(element + " ");
}
}
}