I have an extension method which returns a value. In this case, it should set a specific bit in a byte:
public static byte SetBit(this byte b, int bitNumber, bool value)
{
if (value)
{
return (byte)(b | (1 << bitNumber));
}
return (byte)(b & ~(1 << bitNumber));
}
The returned value needs to be assigned to a variable again, because I cannot combine this
and ref
:
byte myByte = 3;
myByte = myByte.SetBit(1, false);
It is easy to forget the assignment by accident. Someone might think that the method changes the value directly.
For a String.Replace
, ReSharper/Visual Studio(?) is warning me that the Return value of pure method is not used:
Is this also possible for my method in some way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…