title: Overriding implicit operators in C#
body: <p>I use a framework which exposes an abstract class called <code>Value</code>. Through operator overloading, it's possible to assign almost anything to this class's objects, and it works like a charm:</p>
  
  <pre><code>Value a = "hello";
  Value b = 1;
  Value c = true;
  Value d = 3.14;
  </code></pre>
  
  <p>(Note that this is the only way to create instances of <code>Value</code>. There are no public/protected ways to assign values to instances, other than the overloaded operators.)</p>
  
  <p>Right now, I want to override the <code>implicit operator Value(string input)</code> function, so that it XML-sanitizes any string before assigning it.</p>
  
  <p>I have tried inheriting this class and overriding the operator, but have not found a way to feed the sanitized string into the base class's operator function. The following obviously doesn't work:</p>
  
  <pre><code>public override static implicit operator XmlValue(string input)
  {
      string output = sanitize(input);
      XmlValue rv = null;
      ((Value)rv) = output; // this is not possible
      return rv;
  }
  </code></pre>
  
  <p>Is there a way to achieve this? Or alternatively, am I perhaps overthinking the problem and is there a better solution for what I want to do? In any case, I'd like to avoid having to sanitize each and every string before assigning it to a <code>Value</code>; this would be way too error prone.</p>
  
  <p><em>FYI: the <code>Value</code> class is part of the <a href="https://r3c.github.io/cottle/" rel="nofollow noreferrer" title="Cottle framework">Cottle framework</a>.</em></p>