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 readonly

const vs readonly

Scheduled Pinned Locked Moved C#
questionvisual-studio
7 Posts 6 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.
  • P Offline
    P Offline
    psamy
    wrote on last edited by
    #1

    What is the difference between const and read-only variable?

    M C N S 4 Replies Last reply
    0
    • P psamy

      What is the difference between const and read-only variable?

      M Offline
      M Offline
      MIHAI_MTZ
      wrote on last edited by
      #2

      Consts must be asigned at compilation; this means that every time you run the programe they have the same value. Read only can be asigned a value when they are declared and they can't be changed afterwards; this works "readonly int j=i" where i is a variable. So j can have different values the programe is run;

      1 Reply Last reply
      0
      • P psamy

        What is the difference between const and read-only variable?

        C Offline
        C Offline
        coolestCoder
        wrote on last edited by
        #3

        Hi, The difference is -- The value of const is evaluated at compile time and the value of readonly are evaluated at runtime. Means u cannot assign value to const at runtime like this -- const int a = someVariable; but you can do -- readonly int a = someVariable;


        "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


        Anant Y. Kulkarni

        D 1 Reply Last reply
        0
        • C coolestCoder

          Hi, The difference is -- The value of const is evaluated at compile time and the value of readonly are evaluated at runtime. Means u cannot assign value to const at runtime like this -- const int a = someVariable; but you can do -- readonly int a = someVariable;


          "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


          Anant Y. Kulkarni

          D Offline
          D Offline
          Divyang Mithaiwala
          wrote on last edited by
          #4

          Hello Anant,

          Anant Y. Kulkarni wrote:

          const int a = someVariable;

          'someVariable' or must have some constant value? if i changed value of someVariable before upper statement then it will be runtime binding. Isn't it?


          regards, Divyang Mithaiwala System Engineer & Software Developer

          C 1 Reply Last reply
          0
          • D Divyang Mithaiwala

            Hello Anant,

            Anant Y. Kulkarni wrote:

            const int a = someVariable;

            'someVariable' or must have some constant value? if i changed value of someVariable before upper statement then it will be runtime binding. Isn't it?


            regards, Divyang Mithaiwala System Engineer & Software Developer

            C Offline
            C Offline
            coolestCoder
            wrote on last edited by
            #5

            Hi,
            The difference is --

            The value of const is evaluated at compile time and the value of readonly are evaluated at runtime.
            **Means u cannot assign value to const at runtime like this --

            const int a = someVariable;**

            but you can do --

            readonly int a = someVariable;

            I meant that you cannot assign a variable to a const. It should be a constant like -- const int a= 50 I just wrote the opposite.


            "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


            Anant Y. Kulkarni

            1 Reply Last reply
            0
            • P psamy

              What is the difference between const and read-only variable?

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              You also keep in mind the limitations of const vs. read-only. In the case that you have an assembly you distribute, internally or to customers, that contains CONST values. Since CONSTs are set at compile time, if you update your values and distribute the updated assembly unless the apps are recompiled with your new assembly they will still have the old vlaues for the CONST variables.


              only two letters away from being an asset

              1 Reply Last reply
              0
              • P psamy

                What is the difference between const and read-only variable?

                S Offline
                S Offline
                Scott Dorman
                wrote on last edited by
                #7

                Taken from the MSDN[^] documentation for the readonly keyword:

                When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants.

                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