Srinivasan Saripalli
Srinivasan Saripalli

Reputation: 19

Difference between ReadOnlyObservableCollection and ObservableCollection

What is the difference between ReadOnlyObservableCollection and ObservableCollection?

I have the following code snippet:

private ObservableCollection<FeedData> _Feeds = new ObservableCollection<FeedData>();
public ObservableCollection<FeedData> Feeds
{
    get
    {
        return this._Feeds;
    }
}

Can I replace ObservableCollection with ReadOnlyObservableCollection?

Upvotes: 2

Views: 2593

Answers (2)

Tilak
Tilak

Reputation: 30698

ReadOnlyObservableCollection is a readonly wrapper over ObservableCollection.

No changes can be made through this readonly wrapper but all the changes to underlying ObservableCollection are reflected to ReadOnlyObservableCollection.

If ObservableCollection is not exposed as ReadOnly (not the readonly property, but as ReadOnlyObservableCollection), the underlying collection is exposed to modification.

Arrays returned by properties are not write-protected, even if the property is read-only. To keep the array tamper-proof, the property must return a copy of the array. Typically, users will not understand the adverse performance implications of calling such a property. Specifically, they might use the property as an indexed property.

CA1819: Properties should not return arrays

Upvotes: 1

Preet Sangha
Preet Sangha

Reputation: 65506

Try adding to the _Feeds when it's a ReadOnly Collection.

It's a wrapper that prevents you from changing the collection. Of course its just a wrapper so if you have reference to the underlying collection, you can by pass the ReadOnly

The clue is in the name.

Upvotes: 2

Related Questions