const Vs static

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

    const Vs static

    Hi,

    I'm having trouble trying to define the difference between declaring a field
    in a class const or static. i've noted that when referencing them from the
    class that a const is symbolized with a field symbol & a static has a blue
    box. What is the difference? What is the blue box? Has it got to do with
    scope or get/setability?

    Thanks for your thoughts in advance

    Ant
  • Jon Skeet [C# MVP]

    #2
    Re: const Vs static

    Ant <Ant@discussion s.microsoft.com > wrote:[color=blue]
    > I'm having trouble trying to define the difference between declaring a field
    > in a class const or static. i've noted that when referencing them from the
    > class that a const is symbolized with a field symbol & a static has a blue
    > box. What is the difference? What is the blue box? Has it got to do with
    > scope or get/setability?[/color]

    The value of a const is compiled into any class which uses it, rather
    than referencing the value at runtime. The potentially makes it more
    efficient, but at the cost that if they're in different assemblies and
    you change the value of the const, you have to recompile the other
    assembly in order to see the change.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Christof Nordiek

      #3
      Re: const Vs static

      Hi Ant,

      a const has a value, that is known at compiletime. That value can't change
      while runtime and
      can be part of a constant expression.
      static is a member, that belongs to a class/struct not to an instance of
      that class/struct.
      A static field is a variable that exists once per application, whil instance
      variables exist once per instance.
      The value of a static variable can change during runtime.

      Christof

      "Ant" <Ant@discussion s.microsoft.com > schrieb im Newsbeitrag
      news:982CE8A2-6A52-441F-954F-325E688E6A5C@mi crosoft.com...[color=blue]
      > Hi,
      >
      > I'm having trouble trying to define the difference between declaring a
      > field
      > in a class const or static. i've noted that when referencing them from the
      > class that a const is symbolized with a field symbol & a static has a blue
      > box. What is the difference? What is the blue box? Has it got to do with
      > scope or get/setability?
      >
      > Thanks for your thoughts in advance
      >
      > Ant[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: const Vs static

        Ant <Ant@discussion s.microsoft.com > wrote:[color=blue]
        > I'm having trouble trying to define the difference between declaring a field
        > in a class const or static. i've noted that when referencing them from the
        > class that a const is symbolized with a field symbol & a static has a blue
        > box. What is the difference? What is the blue box? Has it got to do with
        > scope or get/setability?[/color]

        The value of a const is compiled into any class which uses it, rather
        than referencing the value at runtime. The potentially makes it more
        efficient, but at the cost that if they're in different assemblies and
        you change the value of the const, you have to recompile the other
        assembly in order to see the change.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Christof Nordiek

          #5
          Re: const Vs static

          Hi Ant,

          a const has a value, that is known at compiletime. That value can't change
          while runtime and
          can be part of a constant expression.
          static is a member, that belongs to a class/struct not to an instance of
          that class/struct.
          A static field is a variable that exists once per application, whil instance
          variables exist once per instance.
          The value of a static variable can change during runtime.

          Christof

          "Ant" <Ant@discussion s.microsoft.com > schrieb im Newsbeitrag
          news:982CE8A2-6A52-441F-954F-325E688E6A5C@mi crosoft.com...[color=blue]
          > Hi,
          >
          > I'm having trouble trying to define the difference between declaring a
          > field
          > in a class const or static. i've noted that when referencing them from the
          > class that a const is symbolized with a field symbol & a static has a blue
          > box. What is the difference? What is the blue box? Has it got to do with
          > scope or get/setability?
          >
          > Thanks for your thoughts in advance
          >
          > Ant[/color]


          Comment

          Working...