How to make sure a parm to a method cannot be changed.
Posted on 2011-02-27
In c++ the word “const” can be used in parameters so that the compiler will error if code is written that will try to change the parm. Users of this method do not have to worry about its contents being changed. How do I do this in c#, even when the term “ref” is not used the contents of the parameter can be changed.
The difference between the two example lines of code is the c++ version has the key word "const". It is this behavior I am trying to obtain in c#.
private StringCollection GetHeaderQuerySet(ref Data4 dataFile, Hashtable hashList)
hashList.Add(“newKey”, “newValue”); //This changes the list even though the list is not passed by reference. Any method calling this method takes a chance the hashlist is changed.
private CStringArray GetHeaderQuerySet(Data4 &dataFile, const HastTable &hashList)
hashList.Add(“newKey”, “newValue”); //Compiler flags this as an error
Thank you for your time and consideration.