0

I would like to create my own implementation of INotifyCollectionChanged but I want the observable collection to be a dictionary. Something like:

MyObservableDictionary<KeyValuePair<TKey, TValue>>

After reading this article on MSDN about generics it seems that you can define that as

public class MyObservableDictionary<T> : INotifyCollectionChanged where T : struct
{
  public event NotifyCollectionChangedEventHandler CollectionChanged;
}

but this

  1. does not enforces that the struct should be of type KeyValuePair and
  2. I don't know how to reference TKey and TValue in the class.

Is there any solution to this?

Themelis
  • 4,048
  • 2
  • 21
  • 45
  • 3
    `MyObservableDictionary`? – CodeCaster Jun 18 '19 at 11:48
  • 4
    `public class MyObservableDictionary : IDictionary, INotifyCollectionChanged where TValue : struct {...}` - class which is dictionary, observable and can have value type (`strict`) as value only. Add `where TKey : struct` if you want key be a struct as well – Dmitry Bychenko Jun 18 '19 at 11:49
  • 2
    There are several examples of `ObservableDictionary` implementations, why create it on your own? Links: [stackoverflow - .NET ObservableDictionary](https://stackoverflow.com/questions/5663395/net-observabledictionary), [Github - kzu/ObservableDictionary.cs](https://gist.github.com/kzu/cfe3cb6e4fe3efea6d24) – Eliahu Aaron Jun 18 '19 at 11:59

2 Answers2

2

Well, you have 2 generic parameters, let them be TKey and TValue:

public class MyObservableDictionary<TKey, TValue> 

Your class implements two interfaces:

  1. "I want the ... collection to be a dictionary" - IDictionary<TKey, TValue>
  2. "I would like to create my own implementation of INotifyCollectionChanged"

Add them:

public class MyObservableDictionary<TKey, TValue> 
  : IDictionary<TKey, TValue>, 
    INotifyCollectionChanged 

Finally, if I've understood you right, you want to restrict both TKey and TValue to be struct only; you can do it with a help of where:

 public class MyObservableDictionary<TKey, TValue> 
  : IDictionary<TKey, TValue>, 
    INotifyCollectionChanged 
  where TKey : struct
  where TValue : struct {
  //TODO: implementation here
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Doesn't that ` where TValue : struct where TKey : struct` means that TKey would be a struct and TValue another one and I want the generic struct `KeyValuePair`? – Themelis Jun 18 '19 at 11:59
  • If I've understood you *right*, you want `TKey` and `TValue` be `struct`, e.g. `MyObservableDictionary`, but not `MyObservableDictionary` (`object` is not `struct`). Please, not that `TKey` and `TValue` can be *arbitrary* `struct`, not necessary the same. – Dmitry Bychenko Jun 18 '19 at 12:02
  • Probably my bad Dmitry, I wanted `TKey` and `TValue` to be parameters of a struct. That's the struct that I want `KeyValuePair`. – Themelis Jun 18 '19 at 12:03
  • 1
    @Themelis: `KeyValuePair` is a *private affair* of the dictionary; you don't have to put any constraint on it. Current .Net (4.8) implements `KeyValuePair – Dmitry Bychenko Jun 18 '19 at 12:07
0

Are you looking for

MyObservableDictionairy<TKey, TValue> : INotifyCollectionChanged where TValue : struct

You can then wrap the key/values into key/value pairs as they are added, or simply have an add method signature like this:

public void Add(KeyValuePair<TKey, TValue> value)
Arthur Heidt
  • 101
  • 4