C# 我可以用受保护/私有静态变量做什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10716276/
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 can I do with a protected/private static variable?
提问by markzzz
I see I can write :
我看到我可以写:
protected static
in my C# class (in my case, an aspx.cs). As well as :
在我的 C# 类中(在我的例子中,一个 aspx.cs)。也 :
private static
What does it means? Static is accessible everywhere. Why protected/private?
这是什么意思?静态无处不在。为什么要保护/私有?
采纳答案by Adam Houldsworth
The definition of static isn't "available everywhere". It is a variable shared across the type it is declared within in the scope of an AppDomain.
静态的定义并非“随处可用”。它是一个在 AppDomain 范围内声明的类型之间共享的变量。
Access Modifiersdo not alter this definition, but obviously affect the scope of access.
访问修饰符不会改变此定义,但显然会影响访问范围。
You are confusing the staticmodifier with access modifiers. A static variable still needs accessibility defined. In your example, private static variables are only accessible within the type it is defined in, protected would be accessible within the type and any derived types.
您将静态修饰符与访问修饰符混淆了。静态变量仍然需要定义可访问性。在您的示例中,私有静态变量只能在其定义的类型内访问,protected 将在类型和任何派生类型内访问。
Just a note, be aware that IIS (hosting ASP.NET applications) recycles worker processes, which will flush any static variable values that are alive at the time.
请注意,请注意 IIS(托管 ASP.NET 应用程序)回收工作进程,这将刷新当时处于活动状态的任何静态变量值。
回答by Guido
staticdoes not mean it is accessible everywhere. You still need protected/privateto define visibility.
static并不意味着它随处可见。您仍然需要protected/private来定义可见性。
回答by Tilak
One use is that you can create private static fields, and expose using public static methods/properties (to apply some custom business logic like singleton, etc)
一种用途是您可以创建私有静态字段,并使用公共静态方法/属性公开(应用一些自定义业务逻辑,如单例等)
回答by Mr.GT
Use protected if you only want the variable to be accessible through certain classes, for instance when using polymorphism and inheritance. Public makes it always visible within the scope and private is pretty obvious.
如果您只想通过某些类访问变量,例如在使用多态和继承时,请使用 protected。Public 使其在范围内始终可见,而 private 则非常明显。
回答by Chetan S
If you declare a variable as a Private then you are not able to access it outside the current class and if declare as a Protected then only the derived class is able to access that variable..In your example the basic meaning of private and Protected is not changing so it does not matter how you declare it Static or simple one...
如果将变量声明为 Private,则无法在当前类之外访问它,如果声明为 Protected,则只有派生类才能访问该变量..在您的示例中,private 和 Protected 的基本含义是没有改变,所以你如何声明它是静态的或简单的并不重要......
class Test
{
protected static int var1;
private static int var2;
}
class MainProgram : Test
{
private static int test;
static void Main(string[] args)
{
Test.var1 = 2;
Test.var2 = 5; //ERROR :: We are not able to access var2 because it is private
}
}
In above code you can see if we want the static variable is accessible only in the current class then you need to make it as a Private.
在上面的代码中,您可以看到我们是否希望静态变量只能在当前类中访问,那么您需要将其设置为 Private。
回答by Shree
private
The type or member can only be accessed by code in the same class or struct.
protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.
Static Modifier
Static methods are called without an instance reference.
private
类型或成员只能被同一类或结构中的代码访问。
protected
类型或成员只能被同一个类或结构体中的代码访问,或者只能被派生类中的代码访问。
静态修饰符
在没有实例引用的情况下调用静态方法。
回答by UDAY SONI
Static is a modifier.. And protected and private are access modifier. Access modifier specify the scope of the variable. Static modifier is used when we want field or method to be singleton thus we don't have to access them by creating the object , rather they can be called through class name directly
static 是修饰符。而protected 和private 是访问修饰符。访问修饰符指定变量的范围。当我们希望字段或方法是单例时使用静态修饰符,因此我们不必通过创建对象来访问它们,而是可以直接通过类名调用它们

