Best practice static class or static variables.
-
Hi, I have a question about static. I have the following class: public class Members { static public string Administrator = "A"; } Because I would mark the variable Administrator as a Const, I changed the class to the following: static public class Members { public const string Administrator = "A"; } In my eyes, the behavior is the same in accessing the variable, only that you are not able to change the Variable. Is this correct? Or do I need to use the keyword readonly instead of const? Than I would have: public class Members { static public readonly string Administrator = "A"; } So, help me out by saying what is the best practice here. Thanks.
-
Hi, I have a question about static. I have the following class: public class Members { static public string Administrator = "A"; } Because I would mark the variable Administrator as a Const, I changed the class to the following: static public class Members { public const string Administrator = "A"; } In my eyes, the behavior is the same in accessing the variable, only that you are not able to change the Variable. Is this correct? Or do I need to use the keyword readonly instead of const? Than I would have: public class Members { static public readonly string Administrator = "A"; } So, help me out by saying what is the best practice here. Thanks.
The difference between read only and const is that one can be set in the constructor ( from memory ) A static class is syntactic sugar, the compiler just barfs if any non static elements exist in there. I use it, because it makes my intentions clear, but it really don't actually give you anything.
Christian Graus Please read this if you don't understand the answer I've given you. If you're still stuck, ask me for more information.