You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to use Guard to ensure that a Dictionary had a minimum number of items. The expected method didn't work:
voidMyMethod(Dictionary<int,string>values){// This is what I hoped to use:Guard.HasSizeGreaterThan(values,1);// Error CS0121 The call is ambiguous between the following methods or properties:// 'Guard.HasSizeGreaterThan<T>(ICollection<T>, int, string)' // and 'Guard.HasSizeGreaterThan<T>(IReadOnlyCollection<T>, int, string)'// This works:Guard.HasSizeGreaterThan((ICollection<KeyValuePair<int,string>>)values,1);// This does not:Guard.HasSizeGreaterThan<ICollection<KeyValuePair<int,string>>>(values,1);// Error CS1503 Argument 1: cannot convert from// 'System.Collections.Generic.Dictionary<int, string>'// to 'System.Span<System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<int, string>>>'}
Typing the working one is more work than writing the if-check manually, though the exception message won't be as useful.
Suggestion:
Make an overload for the methods that take an IDictionary<TKey,TValue>
This does not work. It's ambiguous with IReadOnlyCollection<T>
Make an overload for the methods that take an ICollection
This does not work. It's ambiguous with ICollection<T>
I'm out of ideas except for calling the method something else. E.g. DictionaryHasCountGreaterThan. The problem is that then you must remember to use the correct method.
When C#/.NET gets support for shapes (or whatever it's getting called) something like this would be great:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Problem:
I tried to use Guard to ensure that a
Dictionary
had a minimum number of items. The expected method didn't work:Typing the working one is more work than writing the if-check manually, though the exception message won't be as useful.
Suggestion:
Make an overload for the methods that take an
IDictionary<TKey,TValue>
This does not work. It's ambiguous with
IReadOnlyCollection<T>
Make an overload for the methods that take an
ICollection
This does not work. It's ambiguous with
ICollection<T>
I'm out of ideas except for calling the method something else. E.g.
DictionaryHasCountGreaterThan
. The problem is that then you must remember to use the correct method.When C#/.NET gets support for shapes (or whatever it's getting called) something like this would be great:
Workaround:
Beta Was this translation helpful? Give feedback.
All reactions