Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A private setter for a List, Collection, etc. means that the entire list cannot be replaced by consumers, but it does nothing to protect the public members of the list.</p> <p>For example:</p> <pre><code>public class MyClass { public IList&lt;string&gt; MyList {get; private set;} public MyClass() { MyList = new List&lt;string&gt;(){"One","Two", "Three"}; } } public class Consumer { public void DoSomething() { MyClass myClass = new MyClass(); myClass.MyList = new List&lt;string&gt;(); // This would not be allowed, // due to the private setter myClass.MyList.Add("new string"); // This would be allowed, because it's // calling a method on the existing // list--not replacing the list itself } } </code></pre> <p>In order to prevent consumers from altering the members of the list you could expose it as a Read-only interface, such as <code>IEnumerable&lt;string&gt;</code>, <code>ReadOnlyCollection&lt;string&gt;</code>, or by calling <code>List.AsReadOnly()</code> within the declaring class.</p> <pre><code>public class MyClass { public IList&lt;string&gt; MyList {get; private set;} public MyClass() { MyList = new List&lt;string&gt;(){"One","Two", "Three"}.AsReadOnly(); } } public class Consumer { public void DoSomething() { MyClass myClass = new MyClass(); myClass.MyList = new List&lt;string&gt;(); // This would not be allowed, // due to the private setter myClass.MyList.Add("new string"); // This would not be allowed, the // ReadOnlyCollection&lt;string&gt; would throw // a NotSupportedException } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload