2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-07 09:10:48 +02:00
Inappropriate casts are issues that will lead to unexpected behavior or runtime errors, such as `InvalidCastException`s. The compiler will catch bad casts from one class to another, but not bad casts to interfaces.
2020-06-30 12:47:33 +02:00
2023-06-07 09:10:48 +02:00
It is much better to use the `as` operator because it will return `null` instead of throwing an exception.
2020-06-30 12:47:33 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
2023-06-07 09:10:48 +02:00
public interface IMyInterface
2020-06-30 12:47:33 +02:00
{ /* ... */ }
public class Implementer : IMyInterface
{ /* ... */ }
public class MyClass
{ /* ... */ }
public static class Program
{
public static void Main()
{
var myclass = new MyClass();
2023-06-07 09:10:48 +02:00
var x = (IMyInterface) myclass; // Noncompliant, InvalidCastException is being thrown
var b = myclass is IMyInterface; // Noncompliant, always false
2020-06-30 12:47:33 +02:00
}
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
2023-06-07 09:10:48 +02:00
public interface IMyInterface
2020-06-30 12:47:33 +02:00
{ /* ... */ }
public class Implementer : IMyInterface
{ /* ... */ }
public class MyClass
{ /* ... */ }
public static class Program
{
public static void Main()
{
var myclass = new MyClass();
2023-06-07 09:10:48 +02:00
var x = myclass as IMyInterface; // Compliant, but will always be null
2020-06-30 12:47:33 +02:00
var b = false;
}
}
----
2023-05-03 11:06:20 +02:00
=== Exceptions
2020-06-30 12:47:33 +02:00
No issue is reported if the interface has no implementing class in the assembly.
include::../see.adoc[]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
* Review this cast; in this project there's no type that implements both "{0}" and "{1}".
* Review this cast; in this project there's no type that extends "{0}" and implements "{1}".
* Nullable is known to be empty, this cast throws an exception.
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 11 Dec 2015, 08:58:50 Tamas Vajk wrote:
\[~ann.campbell.2] applied some changes to the description
=== on 11 Dec 2015, 13:57:58 Ann Campbell wrote:
looks good
=== on 21 Jul 2016, 14:39:03 Ann Campbell wrote:
\[~tamas.vajk] I've tweaked the wording of your addition. Please double-check me.
=== on 21 Jul 2016, 15:24:29 Tamas Vajk wrote:
\[~ann.campbell.2] "known-empty" is strange for me, I would probably go with simply "empty" if the original variant was not good.
=== on 21 Jul 2016, 15:38:42 Ann Campbell wrote:
Second attempt made [~tamas.vajk]
=== on 21 Jul 2016, 15:41:08 Tamas Vajk wrote:
\[~ann.campbell.2] perfect, thanks.
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]