Having a friendly debate with a co-worker about this. We have some thoughts about this, but wondering what the SO crowd thinks about this?
Having a friendly debate with a co-worker about this. We have some thoughts about this, but wondering what the SO crowd thinks about this?
Addressing Jared's answer, it would probably just have to be a compile-time feature - the compiler would prohibit you from writing to the variable after the initial declaration (which would have to include an assignment).
Can I see value in this? Potentially - but not a lot, to be honest. If you can't easily tell whether or not a variable is going to be assigned elsewhere in the method, then your method is too long.
For what it's worth, Java has this feature (using the final modifier) and I've very rarely seen it used other than in cases where it has to be used to allow the variable to be captured by an anonymous inner class - and where it is used, it gives me an impression of clutter rather than useful information.
One reason is there is no CLR support for a readonly local. Readonly is translated into the CLR/CLI initonly opcode. This flag can only be applied to fields and has no meaning for a local. In fact, applying it to a local will likely produce unverifiable code.
This doesn't mean that C# couldn't do this. But it would give two different meanings to the same language construct. The version for locals would have no CLR equivalent mapping.
I was that coworker and it wasn't friendly! (just kidding)
I would not eliminate the feature because it's better to write short methods. It's a bit like saying you shouldn't use threads because they're hard. Give me the knife and let me be responsible for not cutting myself.
Personally, I wanted another "var" type keyword like "inv" (invarient) or "rvar" to avoid clutter. I've been studying F# as of late and find the immutable thing appealing.
Never knew Java had this.
Readonly means the only place the instance variable can be set is in the constructor. When declaring a variable locally it doesn't have an instance (it's just in scope), and it can't be touched by the constructor.
Being able to make local variables readonly makes it a lot easier to understand complicated algorithms, since it reduces the number of moving parts.
Since C# doesn't offer it natively for non-compile time constants I use this with implicit casts:
public readonly struct ReadonlyVar<T>
{
private readonly T value;
internal ReadonlyVar(T _value) => value = _value;
public static implicit operator T(ReadonlyVar<T> _readonly) => _readonly.value;
public override string ToString() => "" + value;
}
public static class ReadonlyExt
{
public static ReadonlyVar<T> Readonly<T>(this T _value) => new ReadonlyVar<T>(_value);
}
Usage:
int y = 234;
var x = ( 9000 + y ).Readonly();
y = x;
It is not perfect, since it is possible to assign another ReadonlyVar to it, but this never happened to me unintentionally.
use const keyword to make read only variable.
reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const
public class SealedTest
{
static void Main()
{
const int c = 707;
Console.WriteLine("My local constant = {0}", c);
}
}
I think that's because a function that has a readonly variable may never be called, and there's probably something about it going out of scope, and when would you need to?