I've had a request to look into the feasibility of replacing all of the string ==
operator usages in a reasonably large C# codebase with String.Equals()
method calls that explicitly specify case-sensitivity.
Haven't had much luck figuring out a way to identify all the occurrences in the codebase, though.
System.String.op_Equality
but that doesn't seem to pick up usages inside of LINQ expressions such as .Where(x => x.StringField == stringField)
So I'm a little stumped and wondered if anyone had any ideas on how to search these pesky comparisons out?
String comparison is a common scenario of using both == and equals method. Since java. lang. String class override equals method, It returns true if two String object contains same content but == will only return true if two references are pointing to the same object.
In C++ the == operator is overloaded for the string to check whether both strings are same or not. If they are the same this will return 1, otherwise 0. So it is like Boolean type function.
The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false . The not-equal-to operator ( != ) returns true if the operands don't have the same value; otherwise, it returns false .
Note: When comparing two strings in java, we should not use the == or != operators. These operators actually test references, and since multiple String objects can represent the same String, this is liable to give the wrong answer.
You can use Resharper to find all the usages. Here's what works for me:
operator ==
and right click, select Find Usages
It takes a bit of time but you'll get a nice list of usages, ordered in a tree view. I tried this with Resharper 6.1 in VS2010.
UPDATE
There is a simpler way to do this:
==
in a string comparisonIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With