hosting tips

Concurrent Dictionary for String Comparison in .NET Core

The ConcurrentDictionary class in.NET Core is appropriate for concurrent operations in multi-threaded contexts since it offers a thread-safe collection for storing key-value pairs. To accomplish effective and thread-safe comparison operations, we can use ConcurrentDictionary in conjunction with the relevant techniques when comparing two strings at the same time. This post will explain how to compare two strings in.NET Core concurrently using ConcurrentDictionary, along with the ability to read the strings from the console.

Now let’s use ConcurrentDictionary to implement the string comparison. Using your text editor, open the Program.cs file and make the following changes.

using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
class Program
    static async Task Main(string[] args)
        Console.WriteLine("Enter the first string:");
        string string1 = Console.ReadLine();
        Console.WriteLine("Enter the second string:");
        string string2 = Console.ReadLine();
        ConcurrentDictionary<string, bool> comparisonResults = new ConcurrentDictionary<string, bool>();
        await Task.WhenAll(
            CompareStrings(string1, string2, comparisonResults),
            CompareStrings(string2, string1, comparisonResults)
        bool areEqual1 = comparisonResults[string1];
        bool areEqual2 = comparisonResults[string2];
        Console.WriteLine($"Are '{string1}' and '{string2}' equal? {areEqual1}");
        Console.WriteLine($"Are '{string2}' and '{string1}' equal? {areEqual2}");
    static async Task CompareStrings(string str1, string str2, ConcurrentDictionary<string, bool> comparisonResults)
        bool result = await Task.Run(() => string.Equals(str1, str2));
        comparisonResults.TryAdd(str1, result);

In the code user needs to enter the below data.

  • We prompt the user to enter the first and second strings using Console.ReadLine().
  • We use the entered strings to perform string comparison as before.


In this article, we explored how to use ConcurrentDictionary to compare two strings concurrently in .NET Core. By leveraging ConcurrentDictionary, along with asynchronous programming techniques, we can efficiently compare strings in a thread-safe manner, making it suitable for multi-threaded scenarios. This approach ensures that string comparison operations are performed concurrently without the risk of data corruption or race conditions, enhancing the performance and scalability of our applications. Additionally, by dynamically inputting the strings from the console, we provide flexibility and interactivity to our users.

ASP.NET Core 8 Hosting Recommendation is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.