How to Compare String with Multiple Values in C#?

In this C# tutorial, I have explained how to compare string with multiple values in C# with different methods and examples.

Compare String with Multiple Values in C#

Let us check the different methods to compare strings with multiple values in C#.

1. Using if-else Statements

The simplest way to compare a string against multiple values is using a series of if-else statements. Let’s take a look at an example that compares a given name against some common American names.

using System;

namespace StringCompareExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string nameToCompare = "John";

            if (nameToCompare == "John")
            {
                Console.WriteLine("Hello John!");
            }
            else if (nameToCompare == "Emily")
            {
                Console.WriteLine("Hello Emily!");
            }
            else if (nameToCompare == "Michael")
            {
                Console.WriteLine("Hello Michael!");
            }
            else
            {
                Console.WriteLine("Hello Stranger!");
            }
        }
    }
}

Once you run the code using visual studio, you can see the output below:

compare string with multiple values in c#

2. Using switch Statements

switch statements offer a slightly cleaner approach for comparing strings against multiple values in C#.

using System;

namespace StringCompareExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string nameToCompare = "John";

            switch (nameToCompare)
            {
                case "John":
                    Console.WriteLine("Hello John!");
                    break;
                case "Emily":
                    Console.WriteLine("Hello Emily!");
                    break;
                case "Michael":
                    Console.WriteLine("Hello Michael!");
                    break;
                default:
                    Console.WriteLine("Hello Stranger!");
                    break;
            }
        }
    }
}

Once you run the code, you can see the output like the below screenshot.

how to compare string with multiple values in c#

3. Using Contains Method with an Array or List

Another approach is to use collections like arrays or lists along with the Contains method. This is a more efficient approach if you’re comparing a string against a large set of values.

using System;
using System.Collections.Generic;

namespace StringCompareExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string nameToCompare = "John";
            List<string> nameList = new List<string> { "John", "Emily", "Michael" };

            if (nameList.Contains(nameToCompare))
            {
                Console.WriteLine($"Hello {nameToCompare}!");
            }
            else
            {
                Console.WriteLine("Hello Stranger!");
            }
        }
    }
}

4. Using LINQ

You can also use LINQ (Language-Integrated Query) for more complex comparisons, such as case-insensitive comparison or pattern matching.

using System;
using System.Linq;
using System.Collections.Generic;

namespace StringCompareExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string nameToCompare = "john";
            List<string> nameList = new List<string> { "John", "Emily", "Michael" };

            if (nameList.Any(name => name.Equals(nameToCompare, StringComparison.OrdinalIgnoreCase)))
            {
                Console.WriteLine($"Hello {nameToCompare}!");
            }
            else
            {
                Console.WriteLine("Hello Stranger!");
            }
        }
    }
}

This example, StringComparison.OrdinalIgnoreCase allows for a case-insensitive comparison, which makes “john” and “John” equivalent.

Conclusion

Comparing a string against multiple values in C# can be done in various ways, depending on your specific needs and performance considerations. The if-else and switch statements are straightforward but can become unwieldy with a large number of comparisons. Using collections with the Contains method or using LINQ are more scalable approaches and offer greater flexibility for complex comparisons.

You may like the following tutorials: