.NET 10 Changes Shift Behavior for Small Integer Types in Generic Math
Microsoft's .NET 10 has altered how oversized bit-shift operations behave for small integer types like byte and short when dispatched through generic math interfaces such as IShiftOperators. Unlike concrete C# expressions that promote operands to int, generic methods constrained by IShiftOperators return the original type T, meaning shift counts equal to or exceeding the type's bit width now produce different results than in .NET 9. Developers maintaining libraries that target multiple runtimes should add cross-runtime tests covering shift counts at and beyond the type width to catch behavioral differences. Two mitigation strategies are available: rejecting out-of-range counts via ArgumentOutOfRangeException for inputs like parsers or authorization bitsets, or applying a modulo operation on the count where wrapping behavior is an intentional part of the contract. Microsoft recommends keeping cross-runtime assertions in place until all affected older runtime targets reach end of support.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in