Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is .NET ObservableCollection<T> implemented as a class and not an interface?

In reading about the Observer design pattern, I noticed that it is implemented using interfaces. In Java, the java.util.observable implementation is also a class. Shouldn't the C# and Java versions use interfaces ?

Scott

like image 685
Scott Davies Avatar asked Jul 25 '10 06:07

Scott Davies


People also ask

Why do we use ObservableCollection in C#?

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.

CAN interfaces have implementation C#?

According to the C# Programming Guide, an interface cannot contain an implementation: When a class or struct implements an interface, the class or struct must provide an implementation for all of the members that the interface defines.

Is it necessary to implement all methods of an interface in C#?

Yes, it is mandatory to implement all the methods in a class that implements an interface until and unless that class is declared as an abstract class.

What is the difference between ObservableCollection and BindingList?

The practical difference is that BindingList is for WinForms, and ObservableCollection is for WPF. From a WPF perspective, BindingList isnt properly supported, and you would never really use it in a WPF project unless you really had to.


2 Answers

Well, it implements INotifyCollectionChanged and INotifyPropertyChanged. However, interestingly, it doesn't implement the new IObservable<T> interface from .NET 4.0, which you might have expected.

It would arguably be useful for there to be a generic form of INotifyCollectionChanged... but I don't know of one.

like image 95
Jon Skeet Avatar answered Oct 12 '22 23:10

Jon Skeet


But they DO use interfaces. The ObservableCollection in .NET is an implementation of the interfaces - you are free to ignore it and to your own implementation.

like image 37
TomTom Avatar answered Oct 13 '22 00:10

TomTom