title: Create a List property which cannot be changed externally
body: <p>I have a public class in my VB.NET project which has a <code>List(Of String)</code> property. This list needs to be modified by other classes within the project, but since the class may (at some time in the future) be exposed outside the project, I want it to be unmodifiable at <em>that</em> level. The modification of the existing property within the project will only be done by calling the list's methods (notably <code>.Add</code>, occasionally <code>.Clear</code>), not by a wholesale replacement of the property value with a new List (which is why I have it as a <code>ReadOnly</code> property).</p>
  
  <p>I have come up with <strong><em>a</em></strong> way of doing it, but I'm not sure that it's exactly what you would call "elegant".</p>
  
  <p>It's this:</p>
  
  <pre><code>Friend mlst_ParameterNames As List(Of String) = New List(Of String)
  
  Public ReadOnly Property ParameterNames() As List(Of String)
      Get
          Return New List(Of String)(mlst_ParameterNames)
      End Get
  End Property
  </code></pre>
  
  <p>Now this just works fine and dandy. Any class in the project which accesses the <code>mlst_ParameterNames</code> field directly can modify it as needed, but any procedures which access it through the public property can bang away at modifying it to their heart's content, but will get nowhere since the property procedure is always returning a copy of the list, not the list itself.</p>
  
  <p>But, of course, that carries overhead which is why I feel that it's just... well, viscerally "wrong" at some level, even though it works.</p>
  
  <p>The parameters list will never be huge.  At most it will only contain 50 items, but more commonly less than ten items, so I can't see this ever being a performance killer. However it has of course set me to thinking that someone, with far more VB.NET hours under their belt, may have a much neater and cleaner idea.</p>
  
  <p>Anyone?</p>