ozbear said:
J.B., would wanting to pass an array containing a translation table of
some sort be such an example?
Possibly, but...
If the called method could accept a an array parameter, to allow
different tables to be passed, you might want to be able to describe
that the array contents itself are not to be changed by the routine.
....this is where the feature falls down. The "are not to be changed by
the routine" implies that this is a statement by the caller, and the
caller can NOT say that. The method *being* called can say "I promise
that I won't change the contents of the array", but the caller can't
impose that requirement on arbitrary method calls. Even using
something like AOP the best it could do would be to throw an exception
at runtime, not when compiling.
At compile time the compiler could warn about the method call, saying
it doesn't know if the method will change the contents or not, but
what's the use of that?
So, there's a chicken and egg problem, no code currently has such an
attribute, which means that adding it a bit at a time is likely to be a
pain (you add it to method, but it calls a method that doesn't have it,
so you have to add it to that method as well and so forth and so on,
and then you run into a method that uses reflection and you can't use
it at all). And then there's method's that you didn't write, either
framework or 3rd party, where you can't say for sure whether or not
they violate the contract.
And if you step back from that a bit, you have to ask yourself a
fundamental question: why am I worried about the method changing the
contents?
You said "reference type", as opposed to just "reference", which is
why I mention an array. I am no C++ expert, but even in simple C one
can declare that the formal parameter is const, the object referred
to by the formal parameter is const (but not the parameter itself),
or that both or neither are const.
I'm no C++ expert either, but I see where this feature would be useful
in C++ -- handles and pointers. C++ doesn't really have reference
types, it just has pointers, which are notoriously fragile and error
prone. Adding something to make them less error prone is useful. But
C# HAS reference types and so avoids the confusion caused by pointers.