title: What is the simplest way to access data of an F# discriminated union type in C#?
body: <p>I'm trying to understand how well C# and F# can play together. I've taken some code from the <a href="http://fsharpforfunandprofit.com/posts/recipe-part2/" rel="noreferrer">F# for Fun &amp; Profit blog</a> which performs basic validation returning a discriminated union type:</p>
  
  <pre class="lang-ml prettyprint-override"><code>type Result&lt;'TSuccess,'TFailure&gt; = 
      | Success of 'TSuccess
      | Failure of 'TFailure
  
  type Request = {name:string; email:string}
  
  let TestValidate input =
      if input.name = "" then Failure "Name must not be blank"
      else Success input
  </code></pre>
  
  <p>When trying to consume this in C#; the only way I can find to access the values against Success and Failure (failure is a string, success is the request again) is with big nasty casts (which is a lot of typing, and requires typing actual types that I would expect to be inferred or available in the metadata):</p>
  
  <pre><code>var req = new DannyTest.Request("Danny", "fsfs");
  var res = FSharpLib.DannyTest.TestValidate(req);
  
  if (res.IsSuccess)
  {
      Console.WriteLine("Success");
      var result = ((DannyTest.Result&lt;DannyTest.Request, string&gt;.Success)res).Item;
      // Result is the Request (as returned for Success)
      Console.WriteLine(result.email);
      Console.WriteLine(result.name);
  }
  
  if (res.IsFailure)
  {
      Console.WriteLine("Failure");
      var result = ((DannyTest.Result&lt;DannyTest.Request, string&gt;.Failure)res).Item;
      // Result is a string (as returned for Failure)
      Console.WriteLine(result);
  }
  </code></pre>
  
  <p>Is there a better way of doing this? Even if I have to manually cast (with the possibility of a runtime error), I would hope to at least shorten access to the types (<code>DannyTest.Result&lt;DannyTest.Request, string&gt;.Failure</code>). Is there a better way?</p>