I have two lists in my class and i want to expose one more list as a ReadOnlyCollectoin which is a combination of the two lists.
I am looking for the best way of achieving this. So far i have two options
Option #1
public ReadOnlyCollection<Item> AllItems
{
get
{
var list = new List<Item>();
list.AddRange(List1);
list.AddRange(List2);
return list.AsReadOnly();
}
}
Option #2
Make my two lists ObservableCollections and everytime they are modified, modify a third private list.
and then expose the third list as ReadOnlyCollection.
May be I missing something trivial here. There has to be a better way of achieving this.
Depends on how often the AllItems is invoked in your application and also how big the size of those two lists.
If those are small set and AllItems is going to be invoked very rarely, Option1 is good enough.
If those lists are going to grow fast and AllItems is used frequently, option2 is best option.