readonly issue

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

    #1

    readonly issue

    When I create a reference type as readonly, can I modify its internals
    but not resassing?


    class A
    {
    private int _x;
    public int X
    {
    get; set;
    }
    }

    public static void Main(string [] args){

    readonly A a=new A();;
    A b=new A();
    a.X=5; //is this allowed?
    a=b; // is this allowed?
    }
  • Jon Skeet [C# MVP]

    #2
    Re: readonly issue

    puzzlecracker <ironsel2000@gm ail.comwrote:
    When I create a reference type as readonly
    Careful with the terminology - it's not the type which is being marked
    as readonly, but the variable.
    can I modify its internals but not resassing?
    Exactly. There's no way of saying "this is an immutable instance".

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Peter Duniho

      #3
      Re: readonly issue

      On Mon, 06 Oct 2008 12:05:39 -0700, puzzlecracker <ironsel2000@gm ail.com>
      wrote:
      When I create a reference type as readonly, can I modify its internals
      but not resassing?
      Just try to compile it and see yourself.

      Comment

      • puzzlecracker

        #4
        Re: readonly issue

        On Oct 6, 4:29 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
        puzzlecracker <ironsel2...@gm ail.comwrote:
        When I create a reference type as readonly
        >
        Careful with the terminology - it's not the type which is being marked
        as readonly, but the variable.
        >
        can I modify its internals but not resassing?
        >
        Exactly. There's no way of saying "this is an immutable instance".
        >
        --
        Jon Skeet - <sk...@pobox.co m>
        Web site:http://www.pobox.com/~skeet 
        Blog:http://www.msmvps.com/jon.skeet
        C# in Depth:http://csharpindepth.com
        isn't string immutable? Or Csharp doesn't have the same concept of
        immutability as C++ (const T t; would make t constant, hence no
        modifications are allowed to internals)? I suppose readonly SomeType
        t=new SomeType() is equivalent to SomeTime *const t in C++. Right?

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: readonly issue

          puzzlecracker <ironsel2000@gm ail.comwrote:
          isn't string immutable?
          Yes, but we happen to know that - there's no way of telling either the
          CLR or the compiler that a particular type is immutable.
          Or Csharp doesn't have the same concept of
          immutability as C++ (const T t; would make t constant, hence no
          modifications are allowed to internals)?
          Exactly.
          I suppose readonly SomeType
          t=new SomeType() is equivalent to SomeTime *const t in C++. Right?
          I suspect so, but my C++ is very rusty, so I wouldn't swear to it.

          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: readonly issue

            ozbear <ozbear@bigpond .comwrote:
            Which is a shame. I had written what I thought was an
            immutable class but missed something which rendered it
            mutable. It would be nice if there were an attribute
            or other decoration that one could put on a class
            definition that stated it was (supposed to be) immutable.
            Enforcement of that decoration would, I believe, not be difficult for
            the compiler. All fields readonly so they could only
            be set in a constructor would be adequate.
            I agree that it's a shame that there isn't more support for
            immutability - but I disagree that it's as simple as making all the
            fields readonly:

            public class SupposedlyImmut able
            {
            private readonly List<stringname s = new List<string>();

            public void AddName(string name)
            {
            names.Add(name) ;
            }
            }

            Eric Lippert has a great series of posts about immutability:

            blogs.msdn.com/ericlippert/archive/tags/Immutability/default.aspx

            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            Working...