C# - Static vs. Const

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JPS

    C# - Static vs. Const

    Can someone please refresh my memory here, but what is the difference
    between a Static variable (or method) and a Const?
  • Martin Honnen

    #2
    Re: C# - Static vs. Const

    JPS wrote:
    Can someone please refresh my memory here, but what is the difference
    between a Static variable (or method) and a Const?
    With C#, a static member is a class member:


    const can be applied to members or to variables to indicate that the
    value cannot be modified:

    --

    Martin Honnen --- MVP XML

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: C# - Static vs. Const

      Martin Honnen wrote:
      JPS wrote:
      >Can someone please refresh my memory here, but what is the difference
      >between a Static variable (or method) and a Const?
      >
      With C#, a static member is a class member:
      http://msdn.microsoft.com/en-us/libr...dx(VS.80).aspx
      Meaning that it's shared between all instances of the class.
      >
      const can be applied to members or to variables to indicate that the
      value cannot be modified:
      http://msdn.microsoft.com/en-us/libr...1b(VS.80).aspx
      In C# it means both more and less that simply that the value won't be
      modified.

      More, because it actually has to be a compile-time literal, and the compiler
      is allowed to substitute the literal value everywhere the const variable
      appears. In essence, the const field exists only in the metadata.

      Less, because you can't mark anything as "const" except static fields,
      unlike C++ where variables and access through pointers can be marked "const"
      (can't modify the value) anywhere they appear, including function arguments
      and even the "this" pointer.

      C#'s "readonly" comes a lot closer to a simple marker "cannot be modified
      after creation".


      Comment

      • puzzlecracker

        #4
        Re: C# - Static vs. Const

        Less, because you can't mark anything as "const" except static fields,
        unlike C++ where variables and access through pointers can be marked "const"
        (can't modify the value) anywhere they appear, including function arguments
        and even the "this" pointer.
        >
        I don't think you have to declare a field static if you specify const.
        I believe compilers deduce that automatically.

        correct me if I am wrong.

        Comment

        • Ben Voigt [C++ MVP]

          #5
          Re: C# - Static vs. Const

          puzzlecracker wrote:
          >Less, because you can't mark anything as "const" except static
          >fields, unlike C++ where variables and access through pointers can
          >be marked "const" (can't modify the value) anywhere they appear,
          >including function arguments and even the "this" pointer.
          >>
          >
          I don't think you have to declare a field static if you specify const.
          I believe compilers deduce that automatically.
          >
          correct me if I am wrong.
          No, in fact you can't use the "static" and "const" keywords together. But
          whenever you use "const", you get a static member.


          Comment

          • Peter Duniho

            #6
            Re: C# - Static vs. Const

            On Wed, 22 Oct 2008 07:20:11 -0700, Ben Voigt [C++ MVP]
            <rbv@nospam.nos pamwrote:
            [...]
            Less, because you can't mark anything as "const" except static fields,
            unlike C++ where variables and access through pointers can be marked
            "const"
            (can't modify the value) anywhere they appear, including function
            arguments
            and even the "this" pointer.
            Small nit: you can mark local variables as "const" as well.

            And yes, "const" applied to a field implies "static" as well.

            Pete

            Comment

            • Ben Voigt [C++ MVP]

              #7
              Re: C# - Static vs. Const

              Peter Duniho wrote:
              On Wed, 22 Oct 2008 07:20:11 -0700, Ben Voigt [C++ MVP]
              <rbv@nospam.nos pamwrote:
              >
              >[...]
              >Less, because you can't mark anything as "const" except static
              >fields, unlike C++ where variables and access through pointers can
              >be marked "const"
              >(can't modify the value) anywhere they appear, including function
              >arguments
              >and even the "this" pointer.
              >
              Small nit: you can mark local variables as "const" as well.
              Even smaller nit: they aren't actually variables at that point (I think the
              C# compiler will totally remove local constants during the compile process,
              not even leaving metadata). The expression has to be a compile-time
              literal, which is quite different from C and C++ where const means you can
              have a run-time calculation but the variable can't be changed after it is
              initialized.
              >
              And yes, "const" applied to a field implies "static" as well.
              >
              Pete

              Comment

              • Peter Duniho

                #8
                Re: C# - Static vs. Const

                On Wed, 29 Oct 2008 12:24:51 -0700, Ben Voigt [C++ MVP]
                <rbv@nospam.nos pamwrote:
                >Small nit: you can mark local variables as "const" as well.
                >
                Even smaller nit: they aren't actually variables at that point (I think
                the
                C# compiler will totally remove local constants during the compile
                process,
                not even leaving metadata).
                I know what you mean, but it really just depends on what you definition of
                "variable" is.

                I haven't checked the C# spec, but MSDN definitely still refers to them as
                "variables" even when they are "const". I was simply trying to be
                consistent with MSDN's terminology.

                Pete

                Comment

                Working...