Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a reference type property "readonly"

I have a class Bar with a private field containing the reference type Foo. I would like to expose Foo in a public property, but I do not want the consumers of the property to be able to alter Foo... It should however be alterable internally by Bar, i.e. I can't make the field readonly.

So what I would like is:

      private _Foo;

      public Foo 
      { 
         get { return readonly _Foo; } 
      } 

...which is of course not valid. I could just return a clone of Foo (assumming that it is IClonable), but this is not obvious to the consumer. Should I change the name of the property to FooCopy?? Should it be a GetCopyOfFoo method instead? What would you consider best practice? Thanks!

like image 654
Joel in Gö Avatar asked Mar 25 '09 12:03

Joel in Gö


2 Answers

It sounds like you're after the equivalent of "const" from C++. This doesn't exist in C#. There's no way of indicating that consumers can't modify the properties of an object, but something else can (assuming the mutating members are public, of course).

You could return a clone of the Foo as suggested, or possibly a view onto the Foo, as ReadOnlyCollection does for collections. Of course if you could make Foo an immutable type, that would make life simpler...

Note that there's a big difference between making the field readonly and making the object itself immutable.

Currently, the type itself could change things in both ways. It could do:

_Foo = new Foo(...);

or

_Foo.SomeProperty = newValue;

If it only needs to be able to do the second, the field could be readonly but you still have the problem of people fetching the property being able to mutate the object. If it only needs to do the first, and actually Foo is either already immutable or could be made immutable, you can just provide a property which only has the "getter" and you'll be fine.

It's very important that you understand the difference between changing the value of the field (to make it refer to a different instance) and changing the contents of the object that the field refers to.

like image 75
Jon Skeet Avatar answered Oct 13 '22 01:10

Jon Skeet


Unfortunately, there's no easy way around this in C# at the moment. You could extract the "read only part" of Foo in an interface and let your property return that instead of Foo.

like image 42
Brian Rasmussen Avatar answered Oct 13 '22 00:10

Brian Rasmussen