Hello. I have another question. Kindly look at it.
I have this simple code that is supposed to remove a city from the list of cities if its name does not end with an ‘i’. However, I’m encountering this error:
Program.cs(21,13): error CS0019: Operator ‘!=’ cannot be applied to operands of type ‘char’ and ‘string’.
Your help will be highly appreciated.
using System;
using System.Collections.Generic;
namespace LearnLists
{
class Program
{
static void Main()
{
List<string> citiesList = new List<string> { "Maputo", "Kigali", "Tunis" };
citiesList.Add("Kampala");
citiesList.AddRange(new List<string> {"Nairobi", "Cape Town", "Lagos", "Johannesburg", "Cairo", "Accra", "Addis Ababa", "Dar es Salaam", "Casablanca", "Durban"});
Console.WriteLine($"There are {citiesList.Count} on the list of top cities in Africa.");
//Console.WriteLine($"The first city on the list is {citiesList[0]}.");
//Console.WriteLine(citiesList[0][citiesList[0].Length - 1]);
foreach(string city in citiesList){
//Console.WriteLine(city[city.Length - 1]);
if (city[city.Length - 1] != "i"){
citiesList.Remove(city);
}
}
Console.WriteLine($"There are now {citiesList.Count} on the list of top cities in Africa.");
}
}
}