Equality and Inequality operators are overloaded for strings.So when you do this:
value != "x"
It calls System.String::op_Inequality which calls the String.Equals method:
public static bool operator != (String a, String b)
{
return !String.Equals(a, b);
}
And String.Equals is implemented like this:
public static bool Equals(String a, String b)
{
if ((Object)a == (Object)b)
{
return true;
}
if ((Object)a == null || (Object)b == null)
{
return false;
}
if (a.Length != b.Length)
return false;
return EqualsHelper(a, b);
}
As you can see, it casts the strings to object and returns false if one of them is equal to null.I assume you confused about why comparing string doesn't return null because they are compared by values instead references therefore I share some details.But generally comparing a null object with something never throws a NullReferenceException.That exception is only thrown if you try to call a method on a null object.
if (null != "x")to throw an exception?