In C++, you can pass into a function an object that is const. This forces the code to treat that object as 'read only' (ignoring the fact that you can strip constness away using casts). I'm trying to do the same with C#. I have an interface that the user will implement. One of the methods will be given an object as a parameter. How do I make the object 'const' in the scope of the method and mutable otherwise.
Solution: interfaces. I make an interface where the only action available on a property, is 'get'.
public interface IVal { ... }
public interface IFoo
{
IVal Val { get; }
}
public class Foo : IFoo
{
public IVal Val { get; }
}
I pass by interface and everything is amazing. However, in my concrete class, I would like to have a 'set' available. It would allow me to set a concrete class Value. How would my concrete class look like?
I was thinking of using the 'internal' keyword. I read that it exposes the method/property to code within your own assembly. Is this my answer?
EDIT: To illustrate this better, I added a setter that would automatically cast for me. However, for each property, this is a lot of repetative code:
private Val _val;
public IVal Val
{
get { return this._val; }
set { this._val = value as Val; }
}