Why are const parameters not allowed in C#?

In addition to the other good answers, I’ll add yet another reason why to not put C-style constness into C#. You said:

we mark parameter as const in order to be sure that its state will not be changed in method.

If const actually did that, that would be great. Const doesn’t do that. The const is a lie!

Const doesn’t provide any guarantee that I can actually use. Suppose you have a method that takes a const thing. There are two code authors: the person writing the caller and the person writing the callee. The author of the callee has made the method take a const. What can the two authors assume is invariant about the object?

Nothing. The callee is free to cast away the const and mutate the object, so the caller has no guarantee that calling a method that takes a const actually will not mutate it. Similarly, the callee cannot assume that the contents of the object will not change throughout the action of the callee; the callee could call some mutating method on a non const alias of the const object, and now the so-called const object has changed.

C-style const provides no guarantee that the object will not change, and is therefore broken. Now, C already has a weak type system in which you can do a reinterpret cast of a double into an int if you really want to, so it should not be a surprise that it has a weak type system with respect to const as well. But C# was designed to have a good type system, a type system where when you say “this variable contains a string” that the variable actually contains a reference to a string (or null). We absolutely do not want to put a C-style “const” modifier into the type system because we don’t want the type system to be a lie. We want the type system to be strong so that you can reason correctly about your code.

Const in C is a guideline; it basically means “you can trust me to not try to mutate this thing”. That shouldn’t be in the type system; the stuff in the type system should be a fact about the object that you can reason about, not a guideline to its usage.

Now, don’t get me wrong; just because const in C is deeply broken doesn’t mean that the whole concept is useless. What I would love to see is some actually correct and useful form of “const” annotation in C#, an annotation that both humans and compilers could use to help them understand the code, and that the runtime could use to do things like automatic paralellization and other advanced optimizations.

For example, imagine if you could “draw a box” around a hunk of code and say “I guarantee that this hunk of code performs no mutations to any field of this class” in a way that could be checked by the compiler. Or draw a box that says “this pure method mutates the internal state of the object but not in any way that is observable outside the box”. Such an object could not be safely multi-threaded automatically but it could be automatically memoized. There are all kinds of interesting annotations we could put on code that would enable rich optimizations and deeper understanding. We can do way better than the weak C-style const annotation.

However, I emphasize that this is just speculation. We have no firm plans to put this sort of feature into any hypothetical future version of C#, if there even is one, which we have not announced one way or the other. It is something I would love to see, and something which the coming emphasis on multi-core computing might require, but none of this should be in any way construed to be a prediction or a guarantee of any particular feature or future direction for C#.

Now, if what you want is merely an annotation on the local variable that is a parameter that says “the value of this parameter doesn’t change throughout the method”, then, sure, that would be easily done. We could support “readonly” locals and parameters that would be initialized once, and a compile-time error to change in the method. The variable declared by the “using” statement is already such a local; we could add an optional annotation to all locals and parameters to make them act like “using” variables. It’s never been a very high priority feature so it has never been implemented.

Leave a Comment