title: Can I set the value of decimal variable within an extension method?
body: <p>I have created a <code>void</code> extension method which can be used with <code>decimal</code> data types. I wanted to be able to modify the <code>this</code> parameter variable inside the scope of the method. This is the code for my extension method: </p>
  
  <pre><code>public static void SetAndConvertIfHasValue(this decimal assignTo, double? valueToAssign)
  {
      if (valueToAssign.HasValue)
          assignTo = (decimal)valueToAssign.Value;
      else
          assignTo = 0m;
  }
  </code></pre>
  
  <p>However, when I call it:</p>
  
  <pre><code>data.MyDecimalToSet.SetAndConvertIfHasValue(nullableDouble);
  </code></pre>
  
  <p><code>data.MyDecimalToSet</code> is not set to the value in <code>nullableDouble</code> if it has one. </p>
  
  <p>In debug if I step into the extension method, <code>assignTo</code> is changed to the correct value, this change just doesn't bubble up to <code>data.MyDecimalToSet</code>. </p>
  
  <p>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?</p>