<blockquote>
<p>Is C# type system decidable?</p>
</blockquote>
<p>A type system is &quot;decidable&quot; if the compiler is in theory always able to decide whether the program type checks or not in finite time.</p>
<p><strong>The C# type system is not decidable.</strong></p>
<p>C# has &quot;nominal&quot; subtyping -- that is, you give classes and interfaces <em>names</em> and say what the base classes and interfaces are <em>by name</em> when you declare a class.</p>
<p>C# also has generic types, and, as of C# 4, covariance and contravariance of generic interfaces.</p>
<p>Those three things -- nominal subtyping, generic interfaces, and contravariance -- are sufficient to make a type system undecidable (in the absence of other restrictions on the ways that subtypes may mention each other.)</p>
<p>When this answer was originally written in 2014, that was suspected but not known. The history of this discovery is interesting.</p>
<p>First, the designers of the C# generic type system wondered the same thing, and wrote a paper in 2007 describing different ways in which type checking can go wrong, and what restrictions one can put on a nominal subtyping system that make it decidable.</p>
<p><a href="https://www.microsoft.com/en-us/research/publication/on-decidability-of-nominal-subtyping-with-variance/" rel="noreferrer">https://www.microsoft.com/en-us/research/publication/on-decidability-of-nominal-subtyping-with-variance/</a></p>
<p>A more gentle introduction to the problem can be found on my blog, here:</p>
<p><a href="https://ericlippert.com/2008/05/07/covariance-and-contravariance-part-11-to-infinity-but-not-beyond/" rel="noreferrer">https://ericlippert.com/2008/05/07/covariance-and-contravariance-part-11-to-infinity-but-not-beyond/</a></p>
<p><a href="https://cstheory.stackexchange.com/questions/18846/a-simple-decision-problem-whose-decidability-is-not-known/18866#18866">I have written about this subject on SE sites before</a>; a researcher noticed the problem mentioned in that posting and solved it; we now know that nominal subtyping is in general undecidable if there is generic contravariance thrown into the mix. You can encode a Turing Machine into the type system and force the compiler to emulate its operation, and since the question &quot;does this TM halt?&quot; is undecidable, so must type checking be undecidable.</p>
<p>See <a href="https://arxiv.org/abs/1605.05274" rel="noreferrer">https://arxiv.org/abs/1605.05274</a> for the details.</p>
<blockquote>
<p>Is the C# type system sound?</p>
</blockquote>
<p>A type system is &quot;sound&quot; if we are guaranteed that a program which type checks at compile time has no type errors at runtime.</p>
<p><strong>The C# type system is not sound.</strong></p>
<p>There are many reasons why it is not, but my least favourite is array covariance:</p>
<pre><code>Giraffe[] giraffes = new[] { new Giraffe() };
Animal[] animals = giraffes; // This is legal!
animals[0] = new Tiger(); // crashes at runtime with a type error
</code></pre>
<p>The idea here is that most methods that take arrays only read the array, they do not write it, and it is safe to read an animal out of an array of giraffes. Java allows this, and so the CLR allows it because the CLR designers wanted to be able to implement variations on Java.  C# allows it because the CLR allows it.  The consequence is that <em>every time you write anything into an array of a base class, the runtime must do a check to verify that the array is not an array of an incompatible derived class</em>.  The common case gets slower so that the rare error case can get an exception.</p>
<p>That brings up a good point though: C# is at least well-defined as to the consequences of a type error. Type errors at runtime produce sane behaviour in the form of exceptions. It's not like C or C++ where the compiler can and will blithely generate code that does arbitrarily crazy things.</p>
<p>There are a few other ways in which the C# type system is unsound by design.</p>
<ul>
<li><p>If you consider getting a null reference exception to be a kind of runtime type error, then C# pre C# 8 is very unsound in that it does almost nothing to prevent this kind of error. C# 8 has many improvements in support for detecting nullity errors statically, but the null reference type checking is not sound; it has both false positives and false negatives. The idea is that some compile-time checking is better than none, even if it is not 100% reliable.</p>
</li>
<li><p>Many cast expressions allow the user to override the type system and declare &quot;I know this expression will be of a more specific type at runtime, and if I'm wrong, throw an exception&quot;. (Some casts mean the opposite: &quot;I know this expression is of type X, please generate code to convert it to an equivalent value of type Y&quot;.  Those are generally safe.) Since this is a place where the developer is specifically saying that they know better than the type system, one can hardly blame the type system for the resulting crash.</p>
</li>
</ul>
<p>There are also a handful of features that generate cast-like behaviour even though there is no cast in the code. For example, if you have a list of animals you can say</p>
<pre><code>foreach(Giraffe g in animals)
</code></pre>
<p>and if there is a tiger in there, your program will crash. As the specification notes, the compiler simply inserts a cast on your behalf.  (If you want to loop over all the giraffes and ignore the tigers, that's <code>foreach(Giraffe g in animals.OfType&lt;Giraffe&gt;())</code>.)</p>
<ul>
<li>The <code>unsafe</code> subset of C# makes all bets off; you can break the rules of the runtime arbitrarily with it. Turning off a safety system <strong>turns a safety system off</strong>, so it should not be surprising that C# is not sound when you turn off soundness checking.</li>
</ul>
Is C# type system sound and decidable?
<p>I know that Java's type system is unsound (it fails to type check constructs that are semantically legal) and undecidable (it fails to type check some construct).</p>

<p>For instance, if you copy/paste the following snippet in a class and compile it, the compiler will crash with a <code>StackOverflowException</code> (how apt). This is undecidability.</p>

<pre><code>static class ListX&lt;T&gt; {}
static class C&lt;P&gt; extends ListX&lt;ListX&lt;? super C&lt;C&lt;P&gt;&gt;&gt;&gt; {}
ListX&lt;? super C&lt;Byte&gt;&gt; crash = new C&lt;Byte&gt;();
</code></pre>

<p>Java uses wildcards with type bounds, which are a form of use-site variance. C# on the other hand, uses declaration site variance annotation (with the <code>in</code> and <code>out</code> keywords). It is known that declaration-site variance is weaker than use-site variance (use-site variance can express everything declaration-site variance can, and more -- on the down side, it's much more verbose).</p>

<p>So my question is: Is C# type system sound and decidable? If not, why?</p>