Why doesn't C# have local static variables like C? I miss that!!
为什么C#没有像C这样的局部静态变量?我想念!!
12 个解决方案
#1
You can simulate it using a delegate... Here is my sample code:
您可以使用委托来模拟它...这是我的示例代码:
public Func<int> Increment()
{
int num = 0;
return new Func<int>(() =>
{
return num++;
});
}
You can call it like this:
你可以这样称呼它:
Func<int> inc = Increment();
inc();
#2
Because they screwed up, and left out a useful feature to suit themselves.
因为他们搞砸了,并且遗漏了一个适合自己的有用功能。
All the arguments about how you should code, and what's smart, and you should reconsider your way of life, are pompous defensive excuses.
关于你应该如何编码以及什么是聪明的,你应该重新考虑你的生活方式的所有争论都是浮夸的防御借口。
Sure, C# is pure, and whatchamacallit-oriented. That's why they auto-generate persistent locals for lambda functions. It's all so complicated. I feel so dumb.
当然,C#是纯粹的,而且是以人为本的。这就是他们为lambda函数自动生成持久本地的原因。这一切都很复杂。我觉得很蠢。
Loop scope static is useful and important in many cases.
在许多情况下,循环范围静态是有用且重要的。
Short, real answer, is you have to move local statics into class scope and live with class namespace pollution in C#. Take your complaint to city hall.
简短而真实的答案是,您必须将本地静态移动到类范围内,并在C#中使用类命名空间污染。把你的投诉带到市政厅。
#3
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.
如果你想要一个本地静态变量的等价物,要么创建一个实例变量,要么创建一个静态变量 - 并考虑该方法本身是否应该实际上是该状态的不同类型的一部分。
#4
The MSDN blog entry Why doesn't C# support static method variables? deals with the exact question asked in the original post:
MSDN博客文章为什么C#不支持静态方法变量?处理原帖中提出的确切问题:
There are two reasons C# doesn't have this feature.
C#没有此功能有两个原因。
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]
[作者:Eric Gunnerson]
#5
I'm not nearly as familiar with C as I am C#, but I believe you can accomplish everything you could with a local static, by using a class level static that is only used for one method. Obviously, this comes with some syntactic change, but I believe you can get whatever functionality you need.
我对C语言并不熟悉,因为我是C#,但我相信你可以通过使用仅用于一种方法的类级别静态来完成本地静态的所有操作。显然,这会带来一些语法上的改变,但我相信你可以得到你需要的任何功能。
Additionally, Eric Lippert answers questions like this on his blog a lot. Generally answered in this way: "I am asked "why doesn't C# implement feature X?" all the time. The answer is always the same: because no one ever designed, specified, implemented, tested, documented and shipped that feature." Essentially his answers generally boil down to, it costs money to add any feature, and therefore, many potential features are not implemented because they have not come out on the positive side of the cost benefit analysis.
此外,Eric Lippert在他的博客上回答了很多这样的问题。通常以这种方式回答:“我被问到”为什么C#不能实现功能X?“所有的时间。答案总是一样的:因为没有人设计,指定,实施,测试,记录和发送该功能。 “从本质上讲,他的答案通常归结为,添加任何功能都需要花钱,因此,许多潜在的功能都没有实施,因为它们没有在成本效益分析的积极方面出现。
#6
So you want to use a static local variable in your method? Congratulations! You made another step towards becoming a real programmer.
所以你想在你的方法中使用静态局部变量?恭喜!你朝着成为一名真正的程序员迈出了一步。
Don't listen to all the people telling you that static locals are not "clean", that they impede "readability" and could lead to subtle and hard-to-find "bugs". Nonsense! They just say that because they are wannabe programmers! Lots of them are probably even toying around with an esoteric functional programming language during their free-time. Can you believe it? What a bunch of hipsters!
不要听所有人告诉你静态本地人不“干净”,他们阻碍“可读性”,并可能导致微妙和难以发现的“错误”。废话!他们只是说,因为他们是崇拜程序员!很多人甚至可能在他们的空闲时间使用深奥的函数式编程语言来玩弄它们。你相信吗?真是一堆赶时髦的人!
Real programmers embrace a paradigm I like to call SDD - Side effect Driven Design. Here are some of it's most important laws:
真正的程序员拥抱我喜欢称为SDD的范例 - 侧面效应驱动设计。以下是一些最重要的法律:
Don't be predictable! Never return the same thing from a method twice - even if it's being called with the exact same arguments!
不可预测!永远不要从方法中返回相同的东西两次 - 即使它被使用完全相同的参数调用!
Screw purity - let's get dirty! State, by nature, craves changing, because it is an insatiable monoid in the category of polyamorous endofunctors, i.e. it likes to be touched by as many collaborators as possible. Never miss out on an opportunity to do it the favor!
螺杆纯度 - 让我们变脏!国家本质上渴望改变,因为它是多角形内聚合物类别中永不满足的幺半群,即它喜欢被尽可能多的合作者接触。千万不要错过这个有利于它的机会!
Among the tools used to code in a side effect driven manner are, of course, static local variables. However, as you noticed, C# does not support them. Why? Because over the last two decades Microsoft has been infiltrated by so called Clean Coders that favor maintainability over flexibility and control. Can you even remember the last time you have seen our beloved blue screen? Now guess whose fault is that!
用于以副作用驱动方式编码的工具当然是静态局部变量。但是,正如您所注意到的,C#不支持它们。为什么?因为在过去的二十年里,微软已被所谓的清洁编码器渗透,这种编码器有利于可维护性而不是灵活性和控制。你能记得你最后一次看到我们心爱的蓝屏吗?现在猜猜是谁的错!
But fear not! Real developers don't have to suffer from those poor design decisions. As has been mentioned before it is possible to have local variables that are kind of static with the help of lambdas.
但不要害怕!真正的开发人员不必遭受那些糟糕的设计决策。如前所述,在lambda的帮助下,局部变量可能是静态的。
However, the provided solution wasn't quite satisfactory. Using the previous answer our almost-SDD-compliant code would look something like this:
但是,提供的解决方案并不十分令人满意。使用前面的答案,我们几乎符合SDD的代码看起来像这样:
var inc = Increment();
var zero = inc();
var one = inc();
or
var zero = Increment()();
But that's just silly. Even a wannabe developer can see that Increment()
is not a normal method and will get suspicious. A real programmer, on the other hand, can make it even more SDD-like. He or she knows that we can make a property or field look like a method by giving it the type Func<T>
! We just have to initialize it by executing a lambda that in turn initializes the counter and returns another lambda incrementing the captured counter!
但那太傻了。即使是一个想成为开发人员的人也可以看到Increment()不是一种常规方法,并且会产生怀疑。另一方面,真正的程序员可以使它更像SDD。他或她知道我们可以通过赋予它类型Func
Here it is in proper SDD code:
这是适当的SDD代码:
public Func<int> Increment = new Func<Func<int>>(() =>
{
var num = 0;
return () => num++;
}).Invoke();
(You think the above kinda looks like an IIFE? Yes, you are right and you should be ashamed of yourself.)
(您认为上述类似于IIFE?是的,您是对的,您应该为自己感到羞耻。)
Now every time you call Increment()
it will return something different:
现在,每次调用Increment()时,它都会返回不同的内容:
var zero = Increment();
var one = Increment();
Of course you also can make it so that the counter survives the lifetime of your instance.
当然,您也可以使计数器在您的实例的生命周期中存活。
That'll show them wannabe programmers!
这将表明他们想成为程序员!
#7
C# is a component-oriented language and doesn't have the concept of variables outside the scope of a class or local method. Variables within a method cannot be declared static either, as you may be accustomed to doing in C. However, you can always use a class static variable as a substitute.
C#是一种面向组件的语言,没有类或本地方法范围之外的变量概念。方法中的变量也不能声明为静态,因为您可能习惯于在C中进行操作。但是,您始终可以使用类静态变量作为替代。
As a general practice, there are usually ways to solve programming problems in C# without resorting to using method-level statics. State is generally something you should design into classes and types, not methods.
作为一般实践,通常有一些方法可以解决C#中的编程问题,而无需使用方法级静态。状态通常是你应该设计成类和类型的东西,而不是方法。
#8
Logically, yes. It would be the same as a class-level static member that was only used in that one method. However, a method-level static member would be more encapsulated. If the data stored in a member is only meant to be used by a single method, it should only be accessible by that single method.
从逻辑上讲,是的。它与仅在该方法中使用的类级静态成员相同。但是,方法级静态成员将更加封装。如果存储在成员中的数据仅用于单个方法,则只能通过该单个方法访问。
However, you CAN achieve almost exactly the same effect in C# by creating a nested class.
但是,通过创建嵌套类,您可以在C#中实现几乎完全相同的效果。
#9
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.
如果你不使用它们,以后也会更容易重构。
#10
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++;
}
}
}
#11
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.
您可以使用嵌套类作为解决方法。由于C#将静态变量的范围限制为类,因此可以使用嵌套类作为范围。
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
.
这里Foo支持Increment方法,但它支持私有嵌套类IncrementInternal,它包含静态变量作为成员。当然,在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
.
顺便说一句,如果你想访问IncrementInternal.Increment中的Foo上下文(其他成员和方法),你可以在从Foo调用它时将它作为参数传递给IncrementInternal.Increment。
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.
我认为它比匿名功能或IIFE更清晰。
You can see a live demo here.
你可以在这里看到现场演示。
#12
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.
如果你能想象某种Lippert / Farnsworth混合实体宣布GOOD NEWS EVERYONE !, C#6.0允许使用静态语句。这有效地允许您将静态类方法(以及似乎属性和成员)导入全局范围。
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).
虽然这只在C#6.0中可用,但根据我的理解,生成的程序集应与以前的.NET平台兼容(如果我错了,请纠正我)。
#1
You can simulate it using a delegate... Here is my sample code:
您可以使用委托来模拟它...这是我的示例代码:
public Func<int> Increment()
{
int num = 0;
return new Func<int>(() =>
{
return num++;
});
}
You can call it like this:
你可以这样称呼它:
Func<int> inc = Increment();
inc();
#2
Because they screwed up, and left out a useful feature to suit themselves.
因为他们搞砸了,并且遗漏了一个适合自己的有用功能。
All the arguments about how you should code, and what's smart, and you should reconsider your way of life, are pompous defensive excuses.
关于你应该如何编码以及什么是聪明的,你应该重新考虑你的生活方式的所有争论都是浮夸的防御借口。
Sure, C# is pure, and whatchamacallit-oriented. That's why they auto-generate persistent locals for lambda functions. It's all so complicated. I feel so dumb.
当然,C#是纯粹的,而且是以人为本的。这就是他们为lambda函数自动生成持久本地的原因。这一切都很复杂。我觉得很蠢。
Loop scope static is useful and important in many cases.
在许多情况下,循环范围静态是有用且重要的。
Short, real answer, is you have to move local statics into class scope and live with class namespace pollution in C#. Take your complaint to city hall.
简短而真实的答案是,您必须将本地静态移动到类范围内,并在C#中使用类命名空间污染。把你的投诉带到市政厅。
#3
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.
如果你想要一个本地静态变量的等价物,要么创建一个实例变量,要么创建一个静态变量 - 并考虑该方法本身是否应该实际上是该状态的不同类型的一部分。
#4
The MSDN blog entry Why doesn't C# support static method variables? deals with the exact question asked in the original post:
MSDN博客文章为什么C#不支持静态方法变量?处理原帖中提出的确切问题:
There are two reasons C# doesn't have this feature.
C#没有此功能有两个原因。
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]
[作者:Eric Gunnerson]
#5
I'm not nearly as familiar with C as I am C#, but I believe you can accomplish everything you could with a local static, by using a class level static that is only used for one method. Obviously, this comes with some syntactic change, but I believe you can get whatever functionality you need.
我对C语言并不熟悉,因为我是C#,但我相信你可以通过使用仅用于一种方法的类级别静态来完成本地静态的所有操作。显然,这会带来一些语法上的改变,但我相信你可以得到你需要的任何功能。
Additionally, Eric Lippert answers questions like this on his blog a lot. Generally answered in this way: "I am asked "why doesn't C# implement feature X?" all the time. The answer is always the same: because no one ever designed, specified, implemented, tested, documented and shipped that feature." Essentially his answers generally boil down to, it costs money to add any feature, and therefore, many potential features are not implemented because they have not come out on the positive side of the cost benefit analysis.
此外,Eric Lippert在他的博客上回答了很多这样的问题。通常以这种方式回答:“我被问到”为什么C#不能实现功能X?“所有的时间。答案总是一样的:因为没有人设计,指定,实施,测试,记录和发送该功能。 “从本质上讲,他的答案通常归结为,添加任何功能都需要花钱,因此,许多潜在的功能都没有实施,因为它们没有在成本效益分析的积极方面出现。
#6
So you want to use a static local variable in your method? Congratulations! You made another step towards becoming a real programmer.
所以你想在你的方法中使用静态局部变量?恭喜!你朝着成为一名真正的程序员迈出了一步。
Don't listen to all the people telling you that static locals are not "clean", that they impede "readability" and could lead to subtle and hard-to-find "bugs". Nonsense! They just say that because they are wannabe programmers! Lots of them are probably even toying around with an esoteric functional programming language during their free-time. Can you believe it? What a bunch of hipsters!
不要听所有人告诉你静态本地人不“干净”,他们阻碍“可读性”,并可能导致微妙和难以发现的“错误”。废话!他们只是说,因为他们是崇拜程序员!很多人甚至可能在他们的空闲时间使用深奥的函数式编程语言来玩弄它们。你相信吗?真是一堆赶时髦的人!
Real programmers embrace a paradigm I like to call SDD - Side effect Driven Design. Here are some of it's most important laws:
真正的程序员拥抱我喜欢称为SDD的范例 - 侧面效应驱动设计。以下是一些最重要的法律:
Don't be predictable! Never return the same thing from a method twice - even if it's being called with the exact same arguments!
不可预测!永远不要从方法中返回相同的东西两次 - 即使它被使用完全相同的参数调用!
Screw purity - let's get dirty! State, by nature, craves changing, because it is an insatiable monoid in the category of polyamorous endofunctors, i.e. it likes to be touched by as many collaborators as possible. Never miss out on an opportunity to do it the favor!
螺杆纯度 - 让我们变脏!国家本质上渴望改变,因为它是多角形内聚合物类别中永不满足的幺半群,即它喜欢被尽可能多的合作者接触。千万不要错过这个有利于它的机会!
Among the tools used to code in a side effect driven manner are, of course, static local variables. However, as you noticed, C# does not support them. Why? Because over the last two decades Microsoft has been infiltrated by so called Clean Coders that favor maintainability over flexibility and control. Can you even remember the last time you have seen our beloved blue screen? Now guess whose fault is that!
用于以副作用驱动方式编码的工具当然是静态局部变量。但是,正如您所注意到的,C#不支持它们。为什么?因为在过去的二十年里,微软已被所谓的清洁编码器渗透,这种编码器有利于可维护性而不是灵活性和控制。你能记得你最后一次看到我们心爱的蓝屏吗?现在猜猜是谁的错!
But fear not! Real developers don't have to suffer from those poor design decisions. As has been mentioned before it is possible to have local variables that are kind of static with the help of lambdas.
但不要害怕!真正的开发人员不必遭受那些糟糕的设计决策。如前所述,在lambda的帮助下,局部变量可能是静态的。
However, the provided solution wasn't quite satisfactory. Using the previous answer our almost-SDD-compliant code would look something like this:
但是,提供的解决方案并不十分令人满意。使用前面的答案,我们几乎符合SDD的代码看起来像这样:
var inc = Increment();
var zero = inc();
var one = inc();
or
var zero = Increment()();
But that's just silly. Even a wannabe developer can see that Increment()
is not a normal method and will get suspicious. A real programmer, on the other hand, can make it even more SDD-like. He or she knows that we can make a property or field look like a method by giving it the type Func<T>
! We just have to initialize it by executing a lambda that in turn initializes the counter and returns another lambda incrementing the captured counter!
但那太傻了。即使是一个想成为开发人员的人也可以看到Increment()不是一种常规方法,并且会产生怀疑。另一方面,真正的程序员可以使它更像SDD。他或她知道我们可以通过赋予它类型Func
Here it is in proper SDD code:
这是适当的SDD代码:
public Func<int> Increment = new Func<Func<int>>(() =>
{
var num = 0;
return () => num++;
}).Invoke();
(You think the above kinda looks like an IIFE? Yes, you are right and you should be ashamed of yourself.)
(您认为上述类似于IIFE?是的,您是对的,您应该为自己感到羞耻。)
Now every time you call Increment()
it will return something different:
现在,每次调用Increment()时,它都会返回不同的内容:
var zero = Increment();
var one = Increment();
Of course you also can make it so that the counter survives the lifetime of your instance.
当然,您也可以使计数器在您的实例的生命周期中存活。
That'll show them wannabe programmers!
这将表明他们想成为程序员!
#7
C# is a component-oriented language and doesn't have the concept of variables outside the scope of a class or local method. Variables within a method cannot be declared static either, as you may be accustomed to doing in C. However, you can always use a class static variable as a substitute.
C#是一种面向组件的语言,没有类或本地方法范围之外的变量概念。方法中的变量也不能声明为静态,因为您可能习惯于在C中进行操作。但是,您始终可以使用类静态变量作为替代。
As a general practice, there are usually ways to solve programming problems in C# without resorting to using method-level statics. State is generally something you should design into classes and types, not methods.
作为一般实践,通常有一些方法可以解决C#中的编程问题,而无需使用方法级静态。状态通常是你应该设计成类和类型的东西,而不是方法。
#8
Logically, yes. It would be the same as a class-level static member that was only used in that one method. However, a method-level static member would be more encapsulated. If the data stored in a member is only meant to be used by a single method, it should only be accessible by that single method.
从逻辑上讲,是的。它与仅在该方法中使用的类级静态成员相同。但是,方法级静态成员将更加封装。如果存储在成员中的数据仅用于单个方法,则只能通过该单个方法访问。
However, you CAN achieve almost exactly the same effect in C# by creating a nested class.
但是,通过创建嵌套类,您可以在C#中实现几乎完全相同的效果。
#9
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.
如果你不使用它们,以后也会更容易重构。
#10
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++;
}
}
}
#11
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.
您可以使用嵌套类作为解决方法。由于C#将静态变量的范围限制为类,因此可以使用嵌套类作为范围。
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
.
这里Foo支持Increment方法,但它支持私有嵌套类IncrementInternal,它包含静态变量作为成员。当然,在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
.
顺便说一句,如果你想访问IncrementInternal.Increment中的Foo上下文(其他成员和方法),你可以在从Foo调用它时将它作为参数传递给IncrementInternal.Increment。
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.
我认为它比匿名功能或IIFE更清晰。
You can see a live demo here.
你可以在这里看到现场演示。
#12
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.
如果你能想象某种Lippert / Farnsworth混合实体宣布GOOD NEWS EVERYONE !, C#6.0允许使用静态语句。这有效地允许您将静态类方法(以及似乎属性和成员)导入全局范围。
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).
虽然这只在C#6.0中可用,但根据我的理解,生成的程序集应与以前的.NET平台兼容(如果我错了,请纠正我)。