18

I see I can write :

protected static

in my C# class (in my case, an aspx.cs). As well as :

private static

What does it means? Static is accessible everywhere. Why protected/private?

2
  • stackoverflow.com/questions/135020/… Commented May 23, 2012 at 8:35
  • I know! Sorry, I'm coming from Java. There the things are a bit different... Commented May 23, 2012 at 8:50

7 Answers 7

32

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.

Access Modifiers do not alter this definition, but obviously affect the scope of access.

You are confusing the static modifier 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.

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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...

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.

1 Comment

Source: MSDN (various pages). Always polite to credit your source.
4

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.

1 Comment

@markzzz It doesn't matter, how you access them is the same - one is simply a method call, the other is field/property access.
2

static does not mean it is accessible everywhere. You still need protected/private to define visibility.

Comments

1

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.

Comments

0

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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.