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?