it’s kind a simple question but i have the doubt see the code belows
public static String something;
static void Main(string[] args)
{
try
{
if (something == "blah")
System.Console.Write("ok");
}
catch (Exception)
{
throw;
}
}
i know that doesn’t throw an exception but why? because the variable with the name something it’s null and when you compare you’re trying to get a reference with null. Can someone please explain to me why? Thanks!
The variable, something, doesn’t need to be instanced for that kind of comparison. It knows how to compare itself to a null.
So it doesn’t throw an exception because
null == “string”
is a valid comparison which returns false.