Does C# support the use of static local variables?

后端 未结 12 945
眼角桃花
眼角桃花 2020-12-14 17:28

Related: How do I create a static local variable in Java?


Pardon if this is a duplicate; I was pretty sure this would have been

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 17:44

    Three years later...

    You can approximate it with a captured local variable.

     class MyNose
        {
            private static void Main()
            {
                var myNose= new MyNose();
                var nosePicker = myNose.CreatePicker();
    
                var x = nosePicker();
                var y = nosePicker();
                var z = nosePicker();
            }
    
            public Func CreatePicker()
            {
                int boog = 0;
    
                return () => boog++;
            }
        }
    

提交回复
热议问题