For some reason unknown visualstudio tells me this code is unreachable:
            int? newInt = null;
            string test = newInt.ToString();
            if (test == null)
            {
                //unreachable Code
            }
Thank you for your help! :)
string test = newInt.ToString();
test will never be null if you convert it to string. when you convert it, it will become empty string.
int? newInt = null;
string test = newInt.ToString();
if (test == "")
    {
        Console.WriteLine("Hello World"); //Reaches the code
    }
                        Because:
((int?)null).ToString() == string.Empty
The return value from a nullable int is an empty string. The code in the if block is indeed checking for a null value that could never exist. This only works because int? is a framework type, and the behaviour of ToString() is known and immutable. If you tried this on a user-defined value type, the same assertion could not be made.
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