Reading about string comparison in c# i found many ways to compare 2 strings to see if they are equal.
I was used to ==
coming from c++ but i learned that if you compare an object with a string then ==
defaults to reference value (or something like that).
Then for the Equals()
method lets say i have 2 strings.
string s1 = null;
string s2 = "Hello";
if i do s1.Equals(s2);
I get an null-reference exception cause the first string is null
But if i do
string.equals(s1,s2);
It will simply return false
if one of the values is null
and if both the values are null
it will return true
since both strings would be equal in that they are both null
.
So is there any reason not to always use the string.equals()
method.
Also another thing is that if i type the string.equals()
with a capital S in the string like this String.equals()
then it wll still run the same as if i had it lower case but the IDE (visual studio 2015) will tell me that it can be simplified and when i simplify it it turn it to a lower case s. Why is that?
According to MSDN, the string.Equals
method
Determines whether two String objects have the same value.
This is why string.equals(s1,s2);
would always return false in your case, and if both s1
and s2
are null or have the same value would return true.
Also another thing is that if i type the string.equals() with a capital S in the string like this String.equals() then it wll still run the same as if i had it lower case but the IDE (visual studio 2015) will tell me that it can be simplified and when i simplify it it turn it to a lower case s. Why is that?
The string
is a C# primitive, while System.String
or simple String
is the corresponding FCL (Framework Class Library) type. It is the same like int
and Int32
, where int
is a C# primitive and Int32
is the corresponding FLC type.
Both methods perform these steps:
true
if they do). They do this by checking the object references (it does it by checking (object)a==(object)b
: the ==
operator on two object
variables only checks for reference). Note that this step also returns true
if both objects are null
.null
(and return false
if any of them are)false
if they don't)StringComparison
in one of the overloads) (side note: the byte comparison is implemented quite well and performant even being managed unsafe code... but this is an implementation detail that you shouldn't care about)Whether you use one or the another (if you are sure your instance is not null
) is up to you. Both basically do the same and they are equally performant.
Links to implementation: instance Equals method and static string.Equals
You can't call instance methods on null
instances, which is why it's throwing a NullReferenceException
for you... however the fact that your instance is null
may be important (depending on your specifications), you may want to check for null
before the comparison and act accordingly (using string.Equals
would hide that away, and may be more bug-prone). If it isn't important to your specifications, then using one or the other is simply a design decision.
If you are interested, the operator ==
for two string types is implemented like (source link):
public static bool operator == (String a, String b) {
return String.Equals(a, b);
}
So doing a == b
is the same as doing string.Equals(a,b)
As for String
and string
, the former is System.String
(which requires to have a using System;
on top, or to specify the whole System.String.Equals
), and the latter is a built-in alias (which doesn't require using System;
on top). You can use either, they act exactly the same
If 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