Game Development Reference
In-Depth Information
type checking. The static method uses a performance tuned approach that is
employed throughout the .NET framework. First, a check is performed to see if any
of the strings are null. Then a reference equality check is performed to see if the
two strings refer to the same object. If no result has been returned yet, the virtual
instance method is called.
The C# equality operator == , represented by the MSIL op_Equality , simply makes a call to the
static Equals method of System.String , so you do not have to worry about performance with
this one.
Logical equality is provided through the use of the overloaded Compare method,
which has parameters for locale formatting and case insensitive comparisons.
Unlike the Equals method, which returns a boolean value, the Compare method
returns an integer that describes the lexical difference between the two strings,
with a value of zero stating that the two strings are lexicographically identical.
Being able to perform locale-aware and case insensitive comparisons comes at a
significant performance cost. The cost is dependent on the locale used and the
complexity of the rules related to the locale. Because of the fairly significant per-
formance hit when using the Compare method, it is important to minimize calls to
it whenever possible. One approach is to identify comparisons where case and
locale rules can be ignored, using String.Equals() instead of String.Compare() . This
approach works well for situations where the data originates from back-end or
embedded systems, to name a couple examples. Situations where case and locale
rules cannot be ignored, but binary equality is common, are best served by calling
String.Equals() before String.Compare() . Doing so can result in a considerable per-
formance gain if most of the comparisons exhibit binary equality. The following
code shows this.
string string1 = “This is from one place”;
string string2 = “This is from elsewhere”;
if (string1 == string2 || String.Compare(string1, string2) == 0)
// Handle identical strings
In terms of case insensitive comparisons, String.Compare() is able to perform these
comparisons without allocating new strings, whereas a call to String.Equals() with
calls to String.ToUpper() will result in two new strings being allocated.
Search Nedrilad ::

Custom Search