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
  1. Home
  2. General Programming
  3. C#
  4. const vs static readonly Fields

const vs static readonly Fields

Scheduled Pinned Locked Moved C#
visual-studiotutorialquestionannouncement
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.
  • N Offline
    N Offline
    Nathan Blomquist
    wrote on last edited by
    #1

    Hello, I was just wondering if anyone has a good explanation for the differences between static readonly fields and const fields? The differences I can come up with are: 1. const fields can only be initialized at declaration 2. static readonly can be initialized at declaration or in a static constructor (either directly in the static constructor or through another method called from the static constructor using ref or out) 3. static readonly are changeable at runtime (app start); meaning each time the app is run this field can change Example: public class myClass { static readonly DateTime m_MyStart = DateTime.Now; // this will always be different... } 4. const fields are hard coded into the MSIL at compile time; meaning an app might use a constant from an assembly and later that assembly might change the constants, but the app doesn't realize the update until the app itself is rebuilt. (Can anyone rephrase this better than that? I suck at trying to describe this...) Anyone else have differences or corrections to these? Thanks, Nathan --------------------------- Hmmm... what's a signature?

    E 1 Reply Last reply
    0
    • N Nathan Blomquist

      Hello, I was just wondering if anyone has a good explanation for the differences between static readonly fields and const fields? The differences I can come up with are: 1. const fields can only be initialized at declaration 2. static readonly can be initialized at declaration or in a static constructor (either directly in the static constructor or through another method called from the static constructor using ref or out) 3. static readonly are changeable at runtime (app start); meaning each time the app is run this field can change Example: public class myClass { static readonly DateTime m_MyStart = DateTime.Now; // this will always be different... } 4. const fields are hard coded into the MSIL at compile time; meaning an app might use a constant from an assembly and later that assembly might change the constants, but the app doesn't realize the update until the app itself is rebuilt. (Can anyone rephrase this better than that? I suck at trying to describe this...) Anyone else have differences or corrections to these? Thanks, Nathan --------------------------- Hmmm... what's a signature?

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      That's pretty much it. Const fields can only be used for types where there is a literal representation of the type, and the values are "burned into" the code that uses them. Readonly fields can be used for any type, and the code that uses them references the constructed value at runtime.

      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