c#中`static`是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9410688/
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
What does `static` mean in c#?
提问by Zerotoinfinity
I am really confused with the real meaning of the static keyword in C#. I have gone through different articles on internet but none of them are really helping me to understand it's meaning and other sources are not trusted. I know Stack Overflow has some brilliant minds who can help me understand the real meaning of static like
我真的对 C# 中 static 关键字的真正含义感到困惑。我在互联网上浏览了不同的文章,但没有一篇真正帮助我理解它的含义,其他来源也不可信。我知道 Stack Overflow 有一些聪明的头脑,他们可以帮助我理解静态的真正含义,例如
- When they get initialized.
- static methods, properties, classes and constructors
- Static vs readonly vs constant
- 当它们被初始化时。
- 静态方法、属性、类和构造函数
- 静态 vs 只读 vs 常量
采纳答案by Jon Skeet
In short, static effectively means "associated with a type instead of any one instance of the type". So there's oneset of static variables for a type (within an AppDomain) whether you have 0 instances or a million; you don't need an instance to access a static member, etc.
简而言之,静态实际上意味着“与类型相关联,而不是与该类型的任何一个实例相关联”。因此,无论您有 0 个实例还是 100 万个实例,都存在一组类型(在 AppDomain 内)的静态变量;您不需要实例来访问静态成员等。
The exactpoint of initialization of static variables depends on whether there's also a static constructor or not, but very broadly speaking it's "once, usually before anything significant happens in the class". (See this blog postfor a more detailed description.)
静态变量初始化的确切点取决于是否还有静态构造函数,但从广义上讲,它是“一次,通常在类中发生任何重大事件之前”。(有关更详细的描述,请参阅此博客文章。)
While readonlyfields can be either static or instance (i.e. related to the type or related to an instance of the type), constvalues are alwaysimplicitly static (they're compile-time constants, so it wouldn't make sense to have one copy per instance).
虽然readonly字段可以是静态的或实例的(即与类型相关或与类型的实例相关),但const值始终是隐式静态的(它们是编译时常量,因此每个字段有一个副本是没有意义的)实例)。
You may sometimes see staticbeing described as "shared between all instances of a type" - I personally dislikethat description, as it suggests that there has to be at least one instance... whereas actually, you don't need anyinstances in order to use a static member. I prefer to think of them as entirely separate, rather than being "shared" between instances.
您有时可能会看到static被描述为“在一种类型的所有实例之间共享”-我个人不喜欢这种描述,因为它表明必须至少有一个实例......而实际上,您不需要任何实例使用静态成员。我更愿意将它们视为完全独立的,而不是在实例之间“共享”。
回答by Jan Kratochvil
I can recommend this article, it seems pretty descriptive: Static Keyword Demystified
我可以推荐这篇文章,它看起来很具有描述性: Static Keyword Demystified
I would also recommend an official c# Programming Guide article which covers the various uses of the static keyword. You can go from there since there are a lot of links to different MSDN articles.: Static Classes and Static Class Members (C# Programming Guide)
我还推荐一篇官方的 c# 编程指南文章,其中介绍了 static 关键字的各种用途。您可以从那里开始,因为有很多指向不同 MSDN 文章的链接。:静态类和静态类成员(C# 编程指南)
回答by Tigran
From documentation:
从文档:
The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration
类的静态字段变量初始值设定项对应于按它们在类声明中出现的文本顺序执行的赋值序列
Static members are intializeed on first access to the class and are executed in textual order.
静态成员在第一次访问类时初始化,并按文本顺序执行。
Staticmethods, properties are parts of the classand not instance.
Static方法、属性是类的一部分,而不是实例。
Statichas nothing to do with readonlyor constant. Staticis a way like a member acessed, readonlyand constantis way like a member stored/managed.
Static与readonly或无关constant。Static就像是一个构件的方式acessed,readonly并且constant就像是一个构件的方式存储/管理的。
回答by Lev
A little about constant (const) and readonly:
关于常量(const)和只读:
- constant or const is variable which cannot be modified,and which value is known at compile time.
- readonly is very similar to constant, this cannot be modified either, the difference is that a readonly field can be modified/initialized once in the constructor. After that readonly is the same as constant.
- 常量或常量是不能修改的变量,并且在编译时知道哪个值。
- readonly 与常量非常相似,这也不能修改,区别在于只读字段可以在构造函数中修改/初始化一次。之后 readonly 与常量相同。
Using examples:
使用示例:
constant:
const int a=10; // value cannot be modified, value is known at compile time
But what to do when we want constant field whos value is not known at compile time?
但是当我们想要在编译时不知道其值的常量字段时该怎么办?
e.g const PersonClass a=new PersonClass("name"); // error
The answer is a readonly field:
答案是只读字段:
readonly:
readonly PersonClass a=new PersonClass("name"); // all correct

