C# 静态方法中的全局变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2278153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:58:12  来源:igfitidea点击:

Global variable in a static method

c#staticconsole-application

提问by Leroy Jenkins

This seems basic but Im finding this quite trivial. Simply how would you recommend setting a global variable with a static class (i.e. console-application)?

这看起来很基本,但我发现这很微不足道。您将如何推荐使用静态类(即控制台应用程序)设置全局变量?

To give you a little more background the main method is calling some custom eventhandlers that I hope to get / set the variables.

为了给您更多的背景知识,主要方法正在调用一些我希望获取/设置变量的自定义事件处理程序。

Any ideas or suggestions you have is appreciated.

感谢您的任何想法或建议。

采纳答案by Samuel Neff

Simplest way is

最简单的方法是

public static Object MyGlobalVariable;

which creates a public static field. A little better is:

它创建了一个公共静态字段。稍微好一点的是:

public static Object MyGlobalVariable { get; set; }

Which creates a public static property.

这创建了一个公共静态属性。

回答by Tone

Not 100% sure but you could try a singleton to hold your variables. Without knowing what you are trying to accomplish it's hard to recommend if this solution wouldn't bite you down the road.

不是 100% 确定,但您可以尝试使用单例来保存您的变量。在不知道您要完成什么的情况下,很难推荐此解决方案是否不会让您陷入困境。

http://www.yoda.arachsys.com/csharp/singleton.html

http://www.yoda.arachsys.com/csharp/singleton.html

回答by Aaronaught

There are no global variables in C#. A variable is always locally-scoped. The fundamental unit of code is the class, and within a class you have fields, methods, and properties.

C# 中没有全局变量。变量始终是局部范围的。代码的基本单位是类,在一个类中,你有字段、方法和属性。

You canmimic a "global variable" by making a public static field or property in some class, but you shouldn't. C# makes this difficult for a very good reason; global variables are pure evil. They violate several good principles of OO design - encapsulation, loose coupling, and high cohesion, to name just a few.

可以通过在某个类中创建公共静态字段或属性模拟“全局变量”,但您不应该这样做。C# 使这变得困难是有充分理由的;全局变量是纯粹的邪恶。它们违反了 OO 设计的几个良好原则 - 封装、松耦合和高内聚,仅举几例。

I realize this is a beginner question, but I think it's becausethis is a beginner question that it's so important to be saying this. Now is the best time to start learning what tactics are actively discouraged or even dangerous in C#, and using a static field/property as a global variable is about six of them. There are legitimate uses for these constructs, but passing data around from place to place is not one of them.

我意识到这是一个初学者问题,但我认为这是因为这是一个初学者问题,所以说这个很重要。现在是开始学习在 C# 中积极劝阻甚至危险的策略的最佳时机,使用静态字段/属性作为全局变量大约是其中的六个。这些构造有合法的用途,但从一个地方到另一个地方传递数据不是其中之一。

If two different classes depend upon the same information, then pass the information from the source to the destination. This is usually done either through the constructor or as an argument to the method being called. You should always have one and only one instance that truly "owns" this information; making information "global" means that you can't reason about who or what might be depending on it at any given point in time.

如果两个不同的类依赖于相同的信息,则将信息从源传递到目标。这通常通过构造函数或作为被调用方法的参数来完成。您应该始终拥有一个且只有一个实例真正“拥有”这些信息;使信息“全球化”意味着您无法推断在任何给定时间点可能依赖于它的人或事物。

Please consider this, and try to think about other ways you could share the information that you want to store in a global variable (i.e. by providing it as an argument to a constructor or method). If you're not sure, post an example of what you're trying to do and we'll help out.

请考虑这一点,并尝试考虑其他可以共享要存储在全局变量中的信息的方式(即,将其作为参数提供给构造函数或方法)。如果您不确定,请发布您尝试执行的操作的示例,我们会提供帮助。