23 lines
1.6 KiB
Plaintext
23 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Numbers can be shifted with the `<<` and `>>` https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators#left-shift-operator-[operators], but the right operand of the operation needs to be an `int` or a type that has an https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#102-implicit-conversions[implicit conversion] to `int`. However, when the left operand is https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/interop/using-type-dynamic[dynamic], the compiler's type checking is turned off, so you can pass anything to the right of a shift operator and have it compile. And if the argument can't be implicitly converted to `int` at runtime, then a https://learn.microsoft.com/en-us/dotnet/api/microsoft.csharp.runtimebinder.runtimebinderexception[RuntimeBinderException] will be raised.
|
|
|
|
[source,csharp]
|
|
----
|
|
dynamic d = 5;
|
|
var x = d >> 5.4; // Noncompliant
|
|
x = d << null; // Noncompliant
|
|
x <<= new object(); // Noncompliant
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators#left-shift-operator-[Shift Operators]
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#102-implicit-conversions[Implicit Conversions]
|
|
* https://learn.microsoft.com/en-us/dotnet/api/microsoft.csharp.runtimebinder.runtimebinderexception[RuntimeBinderException Class]
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/interop/using-type-dynamic[Using type dynamic]
|
|
|
|
include::../rspecator-dotnet.adoc[]
|