Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7886997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:27:56+00:00 2026-06-03T05:27:56+00:00

Four quick questions on static variables that the MSDN Faq and other basic guides

  • 0

Four quick questions on static variables that the MSDN Faq and other basic guides seem to neglect.

  1. Is public static the same as static public?

    e.g.
    public static class Globals
    {...}

    vs.

    static public class Globals
    {...}

    Same? Different?

  2. It seems that — like functions — variables in a public static class in C# require public static status to be seen inside other classes via the static class’s named global instance. Why is this? This seems non-intuitive from a naive perspective (it would seem public static class would provide a singular public instance of the class, with any public variables within available). Clearly this is not the case, so I wanted some perspective from C# experts as to why you have to make your member variables in your static class objects static to provide access.

    (Note: The MSDN Faq contains an example of a non-static class with static member vars, but nary a discussion on what if any differences having static members with a public static class has.) (i.e. What if any consequences are there of the doubly static status?)

    e.g.
    public static class Globals
    { public static Camera camera1; }//doubly static

  3. Is there ever a case where public non-static functions within a public static class are appropriate? I can see that you wouldn’t want to expose some things, but wouldn’t you just want to make them private in such a case. (The simpler the example, the better, I’m self-taught in C# and still trying to understand more complex topics like reflection, etc.)

  4. Curiously public enum inside a public static class are visible without a static keyword via the named global instance. Why is the typical static requirement not enforced here? Is there anything I should worry about if I use the visible public enum rather than a public static enum?

    public static class Globals
    { public enum Dummy { Everything=42}; }

    //Enum is visible w/out static!

Thanks in advance. And apologies for the multiple questions, I was on the fence about whether to split this into multiple posts, but it’s all related to C# static use, so I figured one post was most appropriate.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:27:57+00:00Added an answer on June 3, 2026 at 5:27 am

    1: Order doesn’t matter. There are standards of how to order things, for more readability, but as the compiler reads it all – it doesn’t matter at all.

    I personally thing that it would be best writing it as “public static”, instead of “static public”.

    If you download ReSharper to your Visual Studio, it has predefined prioritizing to modifiers such as “static”, “public”, “readonly”, etc… And will suggest you, when you are not following those standards, to correct the order of the modifiers. If you choose to work with a different prioritizing of the modifiers, you can change the ReSharper’s settings to suit your preferred order.

    Other than that – ReSharper does many other wonders and it highly recommended.


    2: Static classes can only contain static members. “static” in the class means that the class can have no instances, and is declared as a being, sort of, as you said. “static” for members means a different thing: Normally, a member would be owned by the instance. Static members, however, are owned by the class – shared among all instances of the class and are used without an actual instance of the class.

    public static class Math
    {
        public static readonly int ZERO = 0;
    }
    

    Here, you can see that ZERO is static, which means it belongs to the class Math.
    So you could do:

    Math.ZERO
    

    Even if the Math class wasn’t static, you would still access the ZERO member via the class itself. The ZERO will not be a member of a Math instance, because it belongs to the class, and not to an instance – hence “static member”.


    3: Number2 sort of answers this one as well. Non-Static class would mean that it can have instances of it and members that belong the instances, but you could also have class-members (static) that would belong to the class itself.

    Example:

    public class Quiz
    {
        public static readonly int FAIL_GRADE = 45;
    
        public int Grade;
        public string StudentName;
    }
    

    So every Quiz has a grade and a student associated with it, but there is also a constant which belongs to the whole class “Quiz” which indicates what grade is considered as Fail Grade.

    In the case above, you could also simply do:

    public const int FAIL_GRADE = 45;
    

    So you can learn that “const” means “static readonly”, logically speaking.
    But in other cases, when you can’t use “const” – you would have to use “static readonly”.
    The “const” can only come before basic types, such as “int”, “float”, “bool”, etc…

    Here is an example where the “static” member is not readonly:

    public static class Student
    {
        public static int TestsTaken = 0;
    
        public string Name;
    
        public int DoQuiz(Quiz quiz, Answers answers)
        {
            TestsTaken++;
    
            // Some answers checking logic and grade returning
        }
    }
    

    In the above example, you can see that the static member of the class Student is used as a counter to how many times instances of Student performed a certain action (DoQuiz). The use of it here is actually not really good programming, since TestsTaken is really something that should be in Quiz, or in School class. But the example for “static” usage stands.


    4: Enums in static classes don’t require the “static” keyword, and in fact you can’t declare a static Enum anywhere. Enum is not considered a member of a class, but a sub-type of it (could be sub-class, interface, enum, etc).

    The fact that an Enum is declared within a class, simply means that if one wishes to use the Enum, he must reference the class as well. It would usually be places within a class for logic purposes, abstraction or encapsulation (would declare it “private” in this case – so it can be used within the class, but not outside of it).

    Example:

    public static class Math
    {
        private enum SpecialSigns
        {
            Sigma,
            Alpha,
            Pi,
            etc
        }
    }
    

    In the above example, the SpecialSigns enum can be used from within the Math class, but is not visible to the outside.

    You could also declare it public, so when one uses Math class, he could also use the SpecialSigns enum. In that case, you could also have SpecialSigns values as return types of methods, or types of public members. You can’t do that when the SpecialSigns is private because the outside code does not have access to it – does not know of it’s existence – and therefore cannot understand it as a return value, member type, etc.

    Still, the SpecialSigns enum is not a member of the class, but only defined within the scope of it’s recognition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have four public websites running on the same database with different schema(Oracle). All
Given four (x,y) pairs that represent the four corners of an arbitrary polygon (quadrilateral)
W3 specifies that only four CSS rules are allowed for table columns (with the
public class Card { public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN,
I have four textfields that bind to the model key path. If a number
There are about four implementations I can think of that are capable of loading
I have an sql query with inner joins of four tables that takes more
First a quick explanation: I am actually dealing with four tables and mining data
I have four integers { a , b , c , d } that
I have four tables containing exactly the same columns, and want to create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.