Calling https://learn.microsoft.com/en-us/dotnet/api/system.object.gettype[GetType()] on a https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types[nullable value type] object returns the underlying value type. Therefore, comparing the returned https://learn.microsoft.com/en-us/dotnet/api/system.type[`Type`] object to `typeof(Nullable<SomeType>)` will either throw an https://learn.microsoft.com/en-us/dotnet/api/system.nullreferenceexception[NullReferenceException] or the result will always be `true` or `false` and can be known at compile time.
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.object.gettype[Object.GetType Method]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types[Nullable value types (C# reference)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.type[Type Class]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.nullreferenceexception[NullReferenceException Class]
\[~ann.campbell.2], could you review this RSPEC? Thanks.
=== on 26 Apr 2016, 17:40:16 Ann Campbell wrote:
\[~tamas.vajk] this rule is a subset of what would be covered by an implementation of RSPEC-2583. When reading it my first thought was that you wrote it stand-alone to cover a R# rule, but there are no references...?
Also, if we retain this RSpec, IMO you should add a compliant solution. From the description and code sample, I'm _guessing_ compliance is a straightforward code change...?
=== on 27 Apr 2016, 08:41:57 Tamas Vajk wrote:
\[~ann.campbell.2] yes, it's a special case of RSPEC-2583.
No, it's not a R# rule. It's the outcome of a bug in one of our rules (RSPEC-2219, added an exception there).
The compliant solution is not straightforward. We can't simply replace the comparison with ``++true++`` or ``++false++`` because that was definitely not what was meant. We could change the ``++typeof(Nullable<int>)++`` to ``++typeof(int)++``, but I don't think that covers the user intent. Most probably the user wanted to check if ``++nullable++`` is a ``++Nullable<T>++`` or not, but that's not possible with ``++GetType++``. And there's no straightforward way to do it, unless you have the type of ``++nullable++`` at compile time.
=== on 27 Apr 2016, 17:44:22 Ann Campbell wrote:
\[~tamas.vajk] I had assumed you'd want to use ``++is++`` (or something similar) instead. Glad I didn't fill in a compliant solution! :-)
For me, the description moves very quickly from
____
Calling GetType() on a nullable object returns the underlying value type.
____
to
____
Thus, comparing the returned Type object to typeof(Nullable<SomeType>) doesn't make sense.