c# - Order readonly List -


I have a readable list, so that I can hide the add method from other classes like:

  Square Fu {Private Readonly List & lt; Bars & gt; _Bars = New list & lt; Times> Public () {this.Bars = _Bars.AsReadOnly (); } Public readline collection & lt; Bars & gt; Bar {receive; Private set; } Public Zero AdBar (vector dimensions) {_Bars.Add (new bar (dimension)); }}  

That is, now I want to order the _Bars field of an example of FU, such as:

  public void OrderBarByVolume () { _Bars.OrderByDescending (O => O. Vol.); // does not do anything _Bars = _Bars.OrderByDescending (O = & gt; O. Vol.) .Oolist (); // error: a readable field can not be assigned}  

Is it possible to use this order and maintain the add-on feature of the hidden list from other classes?

Not with your current implementation, however, if you adjust some things, yes you can The idea of ​​" hide " of underlying data is that you do not have to keep it internally read-only but rather hide as a read only

< Pre> Private list & lt; Bars & gt; _Bars = New list & lt; Bars & gt; (); Public readline calling & lt; Bars & gt; Bar {receive {return _Bars.AsReadOnly (); }} Public Zero Orders (Funk & lt; Bar, Bullet & gt; src) {_Bars = _Bars.OrderByDescending (src); } ... var foo = new Foo (); Foo.OrderBy (x = & gt; x.Volume);

If you experience creating a new ReadOnlyCollection every time you are very expensive, keep your code as you are, but only readonly Modifier Private List & lt; Bars & gt; _Bars = New list & lt; Times> Public Zero Orders (Funk & lt; Bar, Bullet & gt; src) {_Bars = _Bars.OrderByDescending (src) .Oolist (); }


Comments