I have a class that has a getter function for a hashtable called _Parameters.
private Hashtable _Parameters = new Hashtable();
public Hashtable Parameters { get { return _Parameters; } }
_Parameters is not referenced anywhere else in code. Now, since there is no setter function I would think that nothing outside of this class could modify what _Parameters has stored, only read it. However that is not the case. Another class calls this code (where template is an instance of the class mentioned above)
template.Parameters[key] = parameters[key];
This ends up modifying _Parameters. How is this possible? Do setter functions only apply if we are assigning vales with an ‘=’?
No. You’re returning a reference, which can be modified. But you can’t override the reference itself.
Consider using a
ReadOnlyDictionary<TKey, TValue>instead.Consider reading up on Immutable Objects as well. It should explain the subject to you.