I have created a void
extension method which can be used with decimal
data types. I wanted to be able to modify the this
parameter variable inside the scope of the method. This is the code for my extension method:
public static void SetAndConvertIfHasValue(this decimal assignTo, double? valueToAssign)
{
if (valueToAssign.HasValue)
assignTo = (decimal)valueToAssign.Value;
else
assignTo = 0m;
}
However, when I call it:
data.MyDecimalToSet.SetAndConvertIfHasValue(nullableDouble);
data.MyDecimalToSet
is not set to the value in nullableDouble
if it has one.
In debug if I step into the extension method, assignTo
is changed to the correct value, this change just doesn't bubble up to data.MyDecimalToSet
.
At this point I have decided to use a standard method rather than an extension method as a solution to this problem, however I was curious as to why this doesn't work? And whether there is a way around it, or if it simply is impossible?