Concurrent Dictionary for String Comparison in .NET Core

Introduction

In .NET Core, the ConcurrentDictionary class provides a thread-safe collection for storing key-value pairs, making it suitable for concurrent operations in multi-threaded environments. When it comes to comparing two strings concurrently, we can leverage ConcurrentDictionary along with appropriate methods to achieve efficient and thread-safe comparison operations. In this article, we'll explore how to use ConcurrentDictionary for comparing two strings concurrently in .NET Core, with the added functionality of reading the strings from the console.

Now, let's implement the string comparison using ConcurrentDictionary. Open the Program.cs file in your text editor and update it as follows.

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.
    Microsoft visual studio

Conclusion

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.

😊Please consider liking and following me for more articles and if you find this content helpful.👍


Similar Articles
Citiustech Healthcare Technology Pvt Ltd
CitiusTech plays a deep and meaningful role in powering the future of healthcare.