Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Best practice static class or static variables.

Best practice static class or static variables.

Scheduled Pinned Locked Moved C#
questionhelpdiscussion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ESTAN
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • E ESTAN

      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.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups