Operator ‘!=’ cannot be applied to operands of type ‘char’ and ‘string’

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.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"){
      Console.WriteLine($"There are now {citiesList.Count} on the list of top cities in Africa.");


Hi there.

In your program, city[city.Length - 1] will pluck the final character from the city variable (a string) and return it as a char type.

The issue is that what you’re comparing it to, "i", is a string literal. C# won’t allow you to compare a char with a string, hence your error.

You can overcome the issue by using a char literal, which in C# means enclosing the character in single quotes: 'i'.

That will resolve your immediate error, and allow you to make the comparison.

You will, however, run into another issue once that’s fixed because of how you’ve approached the problem. When looping through an iterable item, such as your List<string> here, it’s generally considered unwise to modify the item you’re iterating over… :slight_smile: