I am writing a function that returns a reference to an object of some encapsulated data structure and I want nobody to be able to change the object using that reference, is it possible to do this in c#?
The only way I see is to create two interfaces for this type. One interface just being read only. Then the method just returns an instance of this readonly interface.
If the object that you are returning is immutable, that will work fine.
If not, you can return a wrapper object that only exposes read-only properties.
I don't think there is any built-in way. C# doesn't seem to have the same support for const-correctness that C++ does. You can make the internal members read-only, and that will be a start. But there is more to it than that.
You would make all member functions of your class non-mutator functions, and make all data members properties w/ private setters. When implementing the getters for the properties, copy any classes that come back, and return a new instance, rather than returning a reference to a private member.
class SomeClass
{
public void SomeFunctionThatDoesNotModifyState()
{
}
public int SomeProperty
{
get
{
return someMember; // This is by-value, so no worries
}
}
public SomeOtherClass SomeOtherProperty
{
get
{
return new SomeOtherClass(someOtherMember);
}
}
}
class SomeOtherClass { // .... }
You will have to be very careful that the implementation of SomeOtherClass does a deep copy when you call the copy constructor.
Even after all this, you can't 100% guarantee that someone won't modify your object, because the user can hack into any object via reflections.