title: Read-only ("const"-like) function parameters of C#
body: <p>Coming from a C++ background, I'm used to sticking the <code>const</code> keyword into function definitions to make objects being passed in read-only values. However, I've found out that this is not possible in C# (please correct me if I'm wrong). After some Googling, I arrived at the conclusion that the only way to make a read-only object is to write an interface that only has 'get' properties and pass that in instead. Elegant, I must say.</p>
  
  <pre><code>public interface IFoo
  {
    IMyValInterface MyVal{ get; }
  }
  
  public class Foo : IFoo
  {
    private ConcreteMyVal _myVal;
  
    public IMyValInterface MyVal
    {
      get { return _myVal; }
    }
  }
  </code></pre>
  
  <p>I would pass it into:</p>
  
  <pre><code>public void SomeFunction(IFoo fooVar)
  {
    // Cannot modify fooVar, Excellent!!
  }
  </code></pre>
  
  <p>This is fine. However, in the rest of my code, I would like to modify my object normally. Adding a 'set' property to the interface would break my read-only restriction. I can add a 'set' property to <code>Foo</code> (and not <code>IFoo</code>), but the signature expects an interface rather than a concrete object. I would have to do some casting.</p>
  
  <pre><code>// Add this to class Foo. Might assign null if cast fails??
  set { _myVal = value as ConcreteMyVal; }
  
  // Somewhere else in the code...
  IFoo myFoo = new Foo;
  (myFoo as Foo).MyFoo = new ConcreteMyVal();
  </code></pre>
  
  <p>Is there a more elegant way of replicating <code>const</code> or making read-only function parameters without adding another property or a function?</p>