Description
Description
when adding (and probably performing other math operations on) small numbers (e.g. byte, short) a cast is required as the addition is done using the int add operator. that cast is not necessary with big numbers (e.g. int, long) because they have their own add operator. that cast is optimized away later in the process so it will not effect performance but it will be looking like it would to new developers.
Reproduction Steps
- create a new console application and replace the program.cs contents with:
byte ByteAddition(byte a, byte b)
{
return (byte)(a + b);
}
int IntAddition(byte ai, byte bi)
{
return ai + bi;
}
ByteAddition(1, 2);
IntAddition(3, 4);
- check the actual machine code generated (e.g. using visual studio's disassembly view) and check whether the code is different in
ByteAddition
versusIntAddition
Expected behavior
if the code is anyway exactly the same a cast wont be necessary.
Actual behavior
a cast is necessary.
Regression?
no, it was not different in past versions
Known Workarounds
as stated above the actual executed code is exactly the same only the syntax is a bit misleading
Configuration
.net 8
windows 10
x64
i dont know if on different configuration the performance will be different but i dont think so
Other information
a good solution will probably be just making the the operators from from IAdditionOperators<TSelf, TOther, TResult>
accessible thou unfortunately i do not know how the .net framework is working well enough to judge.