.Net에서 은 및 .Values
속성을 정의합니다. 각 속성은 IEnumerable<>
이 아닌 ICollection<>
입니다. 이는 내게 더 자연스럽게 들릴 것 같습니다..net - dictionary.Keys.Add?
.Keys
또는 IDictionary<K, V>
의 인스턴스의 .Values
에 .Add
또는 .Remove
를 호출하는 합리적인 사용 사례가 있습니까?
.Net에서 은 및 .Values
속성을 정의합니다. 각 속성은 IEnumerable<>
이 아닌 ICollection<>
입니다. 이는 내게 더 자연스럽게 들릴 것 같습니다..net - dictionary.Keys.Add?
.Keys
또는 IDictionary<K, V>
의 인스턴스의 .Values
에 .Add
또는 .Remove
를 호출하는 합리적인 사용 사례가 있습니까?
아니, 아마도 합법적 인 사용 사례는 없습니다. 이것에 대한 합법적 인 이유는 거의 없습니다 (아마 제로).
는 Dictionary<TKey, TValue>
클래스는 차례로 와 NotSupportedException
던졌습니다의 .Keys
에 대한 KeyCollection
반환 "사전에서 파생 된 열쇠 수령 돌연변이하는 것은 허용되지 않습니다는."에 직접 추가하려고 할 때. 나는 레거시 이유로 ICollection
을 반환하고 아마 모든 비용을 피해야한다고 생각합니다.
.Keys
에서 반환 된 ICollection
의 내용이 IDictionary
에 대한 참조가 없다면 아무런 도움이되지 않습니다. ICollection
의 .Add
은이 추가 내용의 의미를 포함하는 IDictionary
을 말해야합니다.
public class StringSet : IDictionary<string, int> {
private readonly Dictionary<string, int> _InternalDictionary = new Dictionary<string, int>();
public int this[string key] {
get { return _InternalDictionary[key]; }
set { _InternalDictionary[key] = value; }
}
private StringCollection _Keys;
public ICollection<string> Keys {
get {
if(_Keys == null) _Keys = new StringCollection(this);
return _Keys;
}
}
ICollection<string> IDictionary<string, int>.Keys {
get {
if(_Keys == null) _Keys = new StringCollection(this);
return _Keys;
}
}
public ICollection<int> Values { get { throw new NotImplementedException();} }
public void Add(string key, int value) { _InternalDictionary.Add(key, value); }
public bool ContainsKey(string key) { return _InternalDictionary.ContainsKey(key); }
public bool Remove(string key) { return _InternalDictionary.Remove(key); }
public bool TryGetValue(string key, out int value) { return _InternalDictionary.TryGetValue(key, out value); }
public void Clear() { throw new NotImplementedException(); }
public void Add(KeyValuePair<string, int> item) { throw new NotImplementedException(); }
public bool Contains(KeyValuePair<string, int> item) { throw new NotImplementedException(); }
public void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex) { throw new NotImplementedException(); }
public bool Remove(KeyValuePair<string, int> item) { throw new NotImplementedException(); }
public int Count { get { return _InternalDictionary.Count; } }
public bool IsReadOnly { get { return false; } }
public IEnumerator<KeyValuePair<string, int>> GetEnumerator() { throw new NotImplementedException(); }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}
public class StringCollection : ICollection<string> {
private readonly StringSet _ContainingSet;
public StringCollection(StringSet set) {
_ContainingSet = set;
}
public void Add(string item) {
if(_ContainingSet.ContainsKey(item)) _ContainingSet[item]++;
else _ContainingSet[item] = 1;
}
public bool Contains(string item) { return _ContainingSet.ContainsKey(item); }
public bool Remove(string item) { throw new NotImplementedException(); }
public void Clear() { throw new NotImplementedException(); }
public void CopyTo(string[] array, int arrayIndex) { throw new NotImplementedException(); }
public int Count { get { return _ContainingSet.Count; } }
public bool IsReadOnly { get { return false; } }
public IEnumerator<string> GetEnumerator() { throw new NotImplementedException(); }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}
은 확실히 사용자의 특정 요구에 대한이 식물 또는 유사한을 구현하는 더 나은 방법이있다 : 아마도 당신은 같은 것을 할 수있는 Set
의 형태를 구현하고 싶었다. 이 유일한 이점은 Keys
StringCollection
에 .Add
을 반환하는 것입니다. 어쨌든 StringSet
을 사용하는 사람들이 부모 StringSet
클래스를 사용하도록 강요하고 싶습니다. 그러나 누군가가 위의 무시 된 행동을 원할 수도 있습니다. 이와
전화를 :
var set = new StringSet();
var keys = set.Keys;
keys.Add("hello");
keys.Add("hello");
keys.Add("world");
Debug.Print("hello: {0}, world: {1}", set["hello"], set["world"]);
'KeyCollection'은 사전에 대한 참조를 보유하고'GetEnumerator'와'Count' (후자를 제공하기 위해 존재하는'ICollection' 인터페이스)에 대한 접근을 주로 전달할 것으로 기대합니다. – supercat
@supercat : 그래,'Dictionary.KeyCollection'과'Dictionary.ValueCollection'는'Dictionary'에 대한 참조를 가지고 있으며, 기본적으로 전달하는 프로퍼티와 메소드를 구현하기 위해'Count','Contains (TValue item)','GetEnumerator()', 등등 ... –
키 또는 값 컬렉션에서 Add
또는 Remove
을 호출하는 합리적인 사용 사례는 생각할 수 없습니다. 결과 사전이 어떻게 보이길 기대합니까?
프레임 워크의 기본 제공 구현 IDictionary<K,V>
은 NotSupportedException
또는 비슷한 것을 던질 것이라고 확신합니다. (그리고 당신은 아마도 자신의 구현에서 똑같은 일을해야 할 것입니다.)
당신은 무엇을 생각하는 의미는 무엇인가를 추가로'Keys'보다는 사전 자체? – DocMax
'ICollection <> '이 제공하는 한 가지는'Count' 속성입니다. 'IEnumerable <>'은'Count()'메소드를 가지고 있습니다. 또한,'IsReadOnly' (다시'ICollection <>'에 의해 정의 됨)의 값을 얻으면, 그 값은'False' 인 것을 알 수 있습니다. 이것은 당신이 그 콜렉션을 수정할 수 없다는 것을 의미합니다. –
@JimMischel : "IsReadOnly (다시 ICollection <>으로 정의)의 가치를 얻으면, **'True' **라는 것을 알았습니다. 컬렉션을 수정할 수 없습니다. "그리고 그것은 분명히 사실입니다. :) –