Four quick questions on static variables that the MSDN Faq and other basic guides seem to neglect.
-
Is
public staticthe same asstatic public?e.g.
public static class Globals
{...}vs.
static public class Globals
{...}Same? Different?
-
It seems that — like functions — variables in a
public static classinC#requirepublic staticstatus to be seen inside other classes via thestaticclass’s named global instance. Why is this? This seems non-intuitive from a naive perspective (it would seempublic static classwould provide a singularpublicinstance of theclass, with any public variables within available). Clearly this is not the case, so I wanted some perspective fromC#experts as to why you have to make your member variables in yourstaticclassobjectsstaticto provide access.(Note: The MSDN Faq contains an example of a non-
staticclasswithstaticmember vars, but nary a discussion on what if any differences havingstaticmembers with apublic static classhas.) (i.e. What if any consequences are there of the doublystaticstatus?)e.g.
public static class Globals
{ public static Camera camera1; }//doubly static -
Is there ever a case where
publicnon-staticfunctions within apublic static classare appropriate? I can see that you wouldn’t want to expose some things, but wouldn’t you just want to make themprivatein such a case. (The simpler the example, the better, I’m self-taught inC#and still trying to understand more complex topics like reflection, etc.) -
Curiously
public enuminside apublic static classare visible without astatickeyword via the named global instance. Why is the typicalstaticrequirement not enforced here? Is there anything I should worry about if I use the visiblepublic enumrather than apublic 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: 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.
Here, you can see that ZERO is static, which means it belongs to the class Math.
So you could do:
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:
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:
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:
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:
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.