Constant objects in C#

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

    #1

    Constant objects in C#

    I've started learning C# (after C++) and I found that
    there is no way to make an object constant. In C++ there
    is a const modifier which allows me to define an object
    which will not change after initialization. In C# it is
    impossible (as far as I know). My question is: Why? In
    C++ checking for changes to const objects is being done
    during compilation. I don't see any reason why can't they
    do the same in C#. Does anybody know??
  • Piotr Bartczak

    #2
    Re: Constant objects in C#

    OK. thanks.[color=blue]
    >-----Original Message-----
    >Hi,
    >
    >There was a big discussion about this in the csharp[/color]
    newsgroup. Please look[color=blue]
    >there instead of putting us all through it again (it got[/color]
    quite heated).[color=blue]
    >
    >Pete[/color]

    Comment

    • Eric Gunnerson [MS]

      #3
      Re: Constant objects in C#

      Piotr,

      If you're just looking for the object equivalent of a constant field, then
      you can use "readonly" instead of const.

      If you're asking about const parameters and methods, you can find some big
      discussions in the archives.

      --
      Eric Gunnerson

      Visit the C# product team at http://www.csharp.net
      Eric's blog is at http://blogs.gotdotnet.com/ericgu/

      This posting is provided "AS IS" with no warranties, and confers no rights.
      "Piotr Bartczak" <pbart@go2.pl > wrote in message
      news:031b01c36d 44$b9d8e930$a00 1280a@phx.gbl.. .[color=blue]
      > I've started learning C# (after C++) and I found that
      > there is no way to make an object constant. In C++ there
      > is a const modifier which allows me to define an object
      > which will not change after initialization. In C# it is
      > impossible (as far as I know). My question is: Why? In
      > C++ checking for changes to const objects is being done
      > during compilation. I don't see any reason why can't they
      > do the same in C#. Does anybody know??[/color]


      Comment

      Working...