Darren Oakey <yo*@darrenoakey.info> wrote:
My favorite way is to have an interface for every class which is
changeable -
ie
interface ConstMyClass
{
int number {get;}
string value {get;}
ConstMyClass CombineWith( ConstMyClass);
...
}
then have your class implement that
ie
class MyClass : ConstMyClass
{
...
}
then most of the places you use it, you can define
void Fun1( ConstMyClass x )
{
}
and be 100% guaranteed that your object isn't going to be manipulated.
Unfortunately it doesn't, actually - because the client can always
check whether or not the instance is *actually* an instance of MyClass,
cast it, and then manipulate it.
What you *can* do is have a wrapper which *contains* an instance of
MyClass, and delegates read access to it, eg:
class ConstMyClass
{
MyClass target;
public ConstMyClass (MyClass target)
{
this.target = target;
}
public int Number
{
get { return target.Number; }
}
}
etc
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too