Why doesn't C# support local static variables

2019-01-21 15:56发布

Why doesn't C# have local static variables like C? I miss that!!

标签: c# static
12条回答
叛逆
2楼-- · 2019-01-21 16:29

I think the idea of local statics is just as easily solved by creating public static fields to the class. Very little logical change don't you think?

If you think it would be a big logical change, I'd be interested to hear how.

class MyClass
{
    public static float MaxDepthInches = 3;

    private void PickNose()
    {
        if (CurrentFingerDepth < MyClass.MaxDepthInches)
        {
            CurrentFingerDepth++;
        }
    }
}
查看更多
我只想做你的唯一
3楼-- · 2019-01-21 16:31

If you can imagine some sort of Lippert/Farnsworth hybrid entity announcing GOOD NEWS EVERYONE!, C# 6.0 allows the using static statement. This effectively allows you to import static class methods (and, it seems, properties and members as well) into the global scope.

In short, you can do something like this:

using NUnit.Framework;
using static Fizz.Buzz;

class Program
{
    [Test]
    public void Main()
    {
        Method();
        int z = Z;
        object y = Y;
        Y = new object();
    }
}


namespace Fizz
{
    class Buzz
    {
        public static void Method()
        {
        }

        public static int Z;

        public static object Y { get; set; }
    }
}   

While this is only available in C# 6.0, from what I understand the generated assemblies should be compatible with previous .NET platforms (correct me if I'm wrong).

查看更多
欢心
4楼-- · 2019-01-21 16:35

The MSDN blog entry Why doesn't C# support static method variables? deals with the exact question asked in the original post:

There are two reasons C# doesn't have this feature.

First, it is possible to get nearly the same effect by having a class-level static, and adding method statics would require increased complexity.

Second, method level statics are somewhat notorious for causing problems when code is called repeatedly or from multiple threads, and since the definitions are in the methods, it's harder to find the definitions.

[Author: Eric Gunnerson]

查看更多
forever°为你锁心
5楼-- · 2019-01-21 16:35

Because static local variables are tied to the method, and the method is shared amongst all instances.

I've had to correct myself and other programmers who expect it to be unique per class instance using the method.

However, if you make it a static class, or static instance of a class, it's syntactically clear whether there's an instance per container-class, or one instance at all.

If you don't use these, it becomes easier to refactor later as well.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-21 16:36

State is generally part of an object or part of a type, not part of a method. (The exception being captured variables, of course.)

If you want the equivalent of a local static variable, either create an instance variable or a static variable - and consider whether the method itself should actually be part of a different type with that state.

查看更多
迷人小祖宗
7楼-- · 2019-01-21 16:40

You can use nested-class as a workaround for this. Since C# is limiting the scope of static variables to classes, you can use nested-class as a scope.

For example:

public class Foo {
    public int Increment() {
        return IncrementInternal.Increment();
    }

    private static class IncrementInternal {
        private static int counter = 0;
        public static int Increment() {
            return counter++;
        }
    }
}

Here Foo supports Increment method, but its support it by the private nested class IncrementInternal which contains the static variable as a member. And of course, counter is not visible in the context (other methods) of Foo.

BTW, if you want to access to Foo context (other members and methods) inside IncrementInternal.Increment, you can pass this as a parameter to IncrementInternal.Increment when you call it from Foo.

To keep the scope as small as possible, I'm suggesting to create a nested class per each such method. And because it is probably not so common, the number of nested classes will stay small enough to maintains it.

I think it is cleaner than anonymous functions or IIFE.

You can see a live demo here.

查看更多
登录 后发表回答