4

I want to write a method which return all key value pairs containing in a IDictionary as Map.EntrySet() does in java.

I tried as:

For example we define IDictionary as:

private IDictionary<T, MutableInt> _map = new Dictionary<T, MutableInt>();    

The method as:

public KeyValuePair<T, MutableInt> KeyValuePairSet()
{
   return KeyValuePair<T, MutableInt>(_map.Keys, _map.Values);
}  

while returning statement, the error raised is:

KeyValuePair<T, MutableInt> is a type but used like a variable

How can this method be implemented?

maliks
  • 1,102
  • 3
  • 18
  • 42
  • return **new** KeyValuePair... However, this will also fail. What exactly do you expect to get in return? a List? an array? – Zohar Peled May 26 '16 at 14:47
  • I actually want to get in return same as what if `_map.entrySet()` returns in Java – maliks May 26 '16 at 14:48
  • You said that already, but though it may come as a surprise, not all c# programmers actually knows Java as well... – Zohar Peled May 26 '16 at 14:50
  • This sounds like a [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), what are you doing that you require re-crateing that method. There may be better ways to get the job you are trying to done. – Scott Chamberlain May 26 '16 at 14:52
  • But I also now mentioning here that `_map.entrySet()` in Java returns same as KeyValuePair in C# – maliks May 26 '16 at 14:53
  • @Taufel no it does not, `_map.entrySet()` returns a **set** of `KeyValuePair`, not a single one. [`KeyValuePair`](https://msdn.microsoft.com/en-us/library/5tbh8a42(v=vs.110).aspx) maps to `Map.Entry` in java. So therefor the `Set>` the function `entrySet()` returns maps to a `ISet>` However, please explain *why* you need to to this, there extremely is likely better ways to do what you are trying to do in C#. – Scott Chamberlain May 26 '16 at 14:54
  • @ScottChamberlain I really means that you right – maliks May 26 '16 at 14:59

2 Answers2

3

Actually it is fairly easily, because IDictionary<TKey, TValue> implmentents the interface IEnumerable<KeyValuePair<TKey, TValue>> all you need to do is declare your HashSet and pass the dictionary in because HashSet<T> has a constructor that takes in a IEnumerable<T> as its parameter.

public ISet<KeyValuePair<T, MutableInt>> KeyValuePairSet()
{
   return new HashSet<KeyValuePair<T, MutableInt>>(_map);
}  
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • 1
    In fact, depending on how OP is planning to use the "set", he may not need the method at all. He may not realize that he can probably just use `_map` directly in a `foreach` loop for example. – sstan May 26 '16 at 14:50
  • it is almost certainly not the right thing to do to return a hash-set of the key/value pairs; to use it directly, you'd need to know both key and value, in which case the original dictionary is moot - you probably want a *list*, or : just a dictionary! – Marc Gravell May 26 '16 at 14:52
3

Given:

private IDictionary<T, MutableInt> map = new Dictionary<T, MutableInt>();

If you want to return IEnumerable of KeyValuePairs:

IEnumerable<KeyValuePair<T, MutableInt>> get_pairs()
{
   return map;
}

If you want to return KeyValuePair of keys and values of map:

KeyValuePair<IEnumerable<T>, IEnumerable<MutableInt>> get_pair()
{
   return new KeyValuePair<IEnumerable<T>, IEnumerable<MutableInt>>(map.Keys, map.Values);
}

If you want to return HashSet of KeyValuePairs:

ISet<KeyValuePair<T, MutableInt>> get_pairs()
{
   return new HashSet<KeyValuePair<T, MutableInt>>(map);
}
Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
  • 2
    He wants the behavior of [`Map.EntrySet()`](https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#entrySet()), which would output a `ISet>`. – Scott Chamberlain May 26 '16 at 14:50
  • `KeyValuePair, IEnumerable>` is just horrible; I can't think of any occasion that would be preferable to just returning the dictionary – Marc Gravell May 26 '16 at 14:53
  • @Riad--statement `return new HashSet(map);` in your last method raises an error i.e. `requires one type of argurment` – maliks May 26 '16 at 15:05
  • Thanks @Taufel, fixed. I omitted specification for HashSet. It is needed to avoid compiling ambiguity. – Riad Baghbanli May 26 '16 at 18:35