Wednesday 15 May 2013

c# - Order readonly List -



c# - Order readonly List -

i have readonly list can hide add together method other classes, this:

class foo { private readonly list<bar> _bars = new list<bar>; public() { this.bars = _bars.asreadonly(); } public readonlycollection<bar> bars { get; private set; } public void addbar(vector dimensions) { _bars.add(new bar(dimensions)); } }

the thing is, want order _bars field of instance of foo, such:

public void orderbarsbyvolume() { _bars.orderbydescending(o => o.volume); //doesn't _bars = _bars.orderbydescending(o => o.volume).tolist(); //error: readonly field cannot assigned }

is possible utilize orderby , maintain add together feature of list hidden other classes?

not current implementation, however, if adjust things yes can. thought of "hiding" underlying info means don't have hold internally read rather expose read only

private list<bar> _bars = new list<bar>(); public readonlycollection<bar> bars { { homecoming _bars.asreadonly(); } } public void orderby(func<bar, bool> src) { _bars = _bars.orderbydescending(src); } ... var foo = new foo(); foo.orderby(x => x.volume);

if sense creating new readonlycollection each time expensive maintain code remove readonly modifier

private list<bar> _bars = new list<bar>; public void orderby(func<bar, bool> src) { _bars = _bars.orderbydescending(src).tolist(); }

c#

No comments:

Post a Comment