In Microsoft .NET there are many ways to compare strings. I would say that most of the code I analyze, I see it done one of these two ways.
bool result = email1 == email2;
bool result = email1.Equals(email2);
Is this the best way to compare strings? The quick answer is no. While this works, they don’t take into consideration localization and globalization. I’ve seen many developers convert the strings to lower or uppercase characters which does affect performance and might not have the results they expect. So, let us see how to properly compare strings while thinking about globalization and performance.
Here is how Wikipedia defines localization and globalization.
In computing, internationalization and are means of adapting computer software to different languages, regional peculiarities, and technical requirements of a target locale. Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting internationalized software for a specific region or language by translating text and adding locale-specific components.
I would say that over 90% of the code projects that I analyze when a company hires me do not take this into account. In this ever-expanding global economy that we live in, this must be built into every line of code, especially dealing with strings that the user ends up seeing. Project Managers and developers who don’t do this from the beginning of the project, are naïve and will end up costing the company a lot of money later. Changing a project to handle multiple languages and locals later will be very painful, and costly and delay the project for many months… even for small projects. I’ve been through this process many times since the ’90s.
Let’s set a baseline for performance that I will refer to later. Here is the performance when using == or Equals() to compare two strings.

As you can see, the performance of these two ways of comparing strings is very close in performance.
String.Compare()
Using string.Compare() for globalization works well and based on the benchmarking I’ve done for my book about code performance, and the best on performance too. Before I show you an example, I need to show the different string comparison globalization choices that can be used.


Jaimin ShethiyaPosted Nov 28, 2019, 1:58 AM
Very useful article sir :)
Sourav Kumar DasPosted Nov 13, 2019, 4:01 AM
Nice useful article SIr.