title: C#'s equivalent to VB.NET's DirectCast
body: <p>Does C# have an equivalent to VB.NET's DirectCast?</p>
  <p>I am aware that it has () casts and the 'as' keyword, but those line up to CType and TryCast.</p>
  <p>To be clear, these keywords do the following;</p>
  <p><strong>CType/() casts</strong>:  If it is already the correct type, cast it, otherwise look for a type converter and invoke it. If no type converter is found, throw an InvalidCastException.</p>
  <p><strong>TryCast/&quot;as&quot; keyword</strong>: If it is the correct type, cast it, otherwise return null.</p>
  <p><strong>DirectCast</strong>: If it is the correct type, cast it, otherwise throw an InvalidCastException.</p>
  <p>After I have spelled out the above, some people have still responded that () is equivalent, so I will expand further upon why this is not true.</p>
  <p>DirectCast only allows for either narrowing or widening conversions on the inheritance tree. It does not support conversions across different branches like () does, i.e.:</p>
  <p>C# - this compiles and runs:</p>
  <pre class="lang-cs prettyprint-override"><code>//This code uses a type converter to go across an inheritance tree
  double d = 10;
  int i = (int)d;
  </code></pre>
  <p>VB.NET - this does NOT COMPILE</p>
  <pre class="lang-vb prettyprint-override"><code>'Direct cast can only go up or down a branch, never across to a different one.
  Dim d As Double = 10
  Dim i As Integer = DirectCast(d, Integer)
  </code></pre>
  <p>The equivalent in VB.NET to my C# code is CType:</p>
  <pre class="lang-vb prettyprint-override"><code>'This compiles and runs
  Dim d As Double = 10
  Dim i As Integer = CType(d, Integer)
  </code></pre>