rspec/rules/S3610/csharp/rule.adoc

22 lines
821 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Calling ``++GetType()++`` on a nullable object returns the underlying value type. Thus, comparing the returned ``++Type++`` object to ``++typeof(Nullable<SomeType>)++`` doesn't make sense. The comparison either throws an exception or the result can be known at compile time.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
int? nullable = 42;
bool comparison = nullable.GetType() == typeof(Nullable<int>); // Noncompliant, always false
comparison = nullable.GetType() != typeof(Nullable<int>); // Noncompliant, always true
nullable = null;
comparison = nullable.GetType() != typeof(Nullable<int>); // Noncompliant, calling GetType on a null always throws an exception
----
ifdef::env-github,rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]