Why does C# disallow readonly local variables?

后端 未结 13 1169
萌比男神i
萌比男神i 2020-11-27 17:17

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?

相关标签:
13条回答
  • 2020-11-27 17:57

    c# already has a readonly var, albeit in a somewhat different syntax:

    Consider the following lines:

    var mutable = myImmutableCalculationMethod();
    readonly var immutable = mutable; // not allowed in C# 8 and prior versions
    return immutable;
    

    Compare with:

    var mutable = myImmutableCalculationMethod();
    string immutable() => mutable; // allowed in C# 7
    return immutable();
    

    Admittedly, the first solution could possibly be less code to write. But the 2nd snippet will make the readonly explicit, when referencing the variable.

    0 讨论(0)
  • 2020-11-27 17:58

    A proposal readonly locals and parameters for was briefly discussed by the C# 7 design team. From C# Design Meeting Notes for Jan 21, 2015:

    Parameters and locals can be captured by lambdas and thereby accessed concurrently, but there's no way to protect them from shared-mutual-state issues: they can't be readonly.

    In general, most parameters and many locals are never intended to be assigned to after they get their initial value. Allowing readonly on them would express that intent clearly.

    One problem is that this feature might be an "attractive nuisance". Whereas the "right thing" to do would nearly always be to make parameters and locals readonly, it would clutter the code significantly to do so.

    An idea to partly alleviate this is to allow the combination readonly var on a local variable to be contracted to val or something short like that. More generally we could try to simply think of a shorter keyword than the established readonly to express the readonly-ness.

    Discussion continues in the C# Language Design repo. Vote to show your support. https://github.com/dotnet/csharplang/issues/188

    0 讨论(0)
  • 2020-11-27 18:00

    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);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 18:00

    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?

    0 讨论(0)
  • 2020-11-27 18:04

    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.

    0 讨论(0)
  • 2020-11-27 18:05

    It is an oversight for c# language designer. F# has val keyword and it is based on CLR. There is no reason C# can't have the same language feature.

    0 讨论(0)
提交回复
热议问题