the given expression is always of the provided type - no, it is not

using System;

namespace ResharperTest
    class Program
        static void Main(string[] args)
            String test = null;
            Random rnd = new Random();

            test = (rnd.NextDouble() > 0.5) ? "String" : null;

            if (test is String)
                Console.WriteLine("Not a String");

Resharper is telling me, that (test is String) is always of the provided type ("The given expression is always of the provided type"). But that is not true. Is it a bug?

I am using ReSharper 6.1 C# Edition Build 6.1.1000.82
Comment actions Permalink

First I thougt you where wrong as I thought is does not check for null but I was wrong.
In the msdn help you will find that one

An is expression evaluates to true if both of the following conditions are met:

  • expression is not null.
  • expression can be cast to type. That is, a cast expression of the form
    will complete without throwing an exception. For more information, see 7.6.6 Cast expressions.

And as your string variable will hold a null reference sometimes it will fail here.
So yes seems like a bug.

Comment actions Permalink

Thanks Sven, I have created an issue: RSRP-312505


Please sign in to leave a comment.