2023-05-03 11:06:20 +02:00
== Why is this an issue?
2020-06-30 12:47:33 +02:00
include::../description.adoc[]
2023-10-11 12:03:57 +02:00
=== Exceptions
2020-06-30 12:47:33 +02:00
2023-10-11 12:03:57 +02:00
Issues are not raised against the https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions[default literal].
== How to fix it
To fix your code remove the unnecessary casting expression.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
2020-06-30 12:47:33 +02:00
----
public int Example(int i)
{
return (int) (i + 42); // Noncompliant
}
public IEnumerable<int> ExampleCollection(IEnumerable<int> coll)
{
return coll.Reverse().OfType<int>(); // Noncompliant
}
----
2023-10-11 12:03:57 +02:00
==== Compliant solution
2020-06-30 12:47:33 +02:00
2023-10-11 12:03:57 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2020-06-30 12:47:33 +02:00
----
public int Example(int i)
{
return i + 42;
}
public IEnumerable<int> ExampleCollection(IEnumerable<int> coll)
{
return coll.Reverse();
}
----
2023-05-25 14:18:12 +02:00
[source,csharp]
2020-06-30 12:47:33 +02:00
----
bool b = (bool)default; // Doesn't raise an issue
----
2021-06-02 20:44:38 +02:00
2023-10-11 12:03:57 +02:00
== Resources
=== Documentation
* Microsoft - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions[Casting and type conversions]
* Wikipedia - https://en.wikipedia.org/wiki/Type_conversion[Type Conversion]
* Wikipedia - https://en.wikipedia.org/wiki/Strong_and_weak_typing[Strong and Weak Typing]
* Wikipedia - https://en.wikipedia.org/wiki/Polymorphism_(computer_science)[ Polymorphism (Computer Science)]
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)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]