Program Tip

StringDictionary 대 사전

programtip 2020. 10. 20. 08:03
반응형

StringDictionary 대 사전


누구든지 System.Collections.Specialized.StringDictionary 개체와 System.Collections.Generic.Dictionary의 실질적인 차이점이 무엇인지 알고 있습니까?

나는 과거에 어느 것이 더 나은 성능을 발휘할 것인지, Linq와 더 잘 작동하는지, 또는 다른 이점을 제공 할 것인지에 대한 많은 생각없이 두 가지를 모두 사용했습니다.

왜 내가 다른 것을 사용 해야하는지에 대한 생각이나 제안이 있습니까?


Dictionary<string, string>보다 현대적인 접근 방식입니다. 구현 IEnumerable<T>하고 LINQy 항목에 더 적합합니다.

StringDictionary구식입니다. 제네릭 시대 이전에 거기에있었습니다. 레거시 코드와 인터페이스 할 때만 사용합니다.


또 다른 요점.

이것은 null을 반환합니다.

StringDictionary dic = new StringDictionary();
return dic["Hey"];

예외가 발생합니다.

Dictionary<string, string> dic = new Dictionary<string, string>();
return dic["Hey"];

StringDictionary는 거의 쓸모가 없다고 생각합니다. 제네릭 이전의 프레임 워크 v1.1에 존재했기 때문에 당시에는 (비 제네릭 사전과 비교했을 때) 우월한 버전 이었지만이 시점에서는 특별한 이점이 없다고 생각합니다. 사전에.

그러나 StringDictionary에는 단점이 있습니다. StringDictionary는 키 값을 자동으로 소문자로 지정하며이를 제어하기위한 옵션이 없습니다.

보다:

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/59f38f98-6e53-431c-a6df-b2502c60e1e9/


Reed Copsey가 지적했듯이 StringDictionary는 키 값을 소문자로 지정합니다. 나에게 이것은 완전히 예상치 못한 일이었고 쇼 스토퍼입니다.

private void testStringDictionary()
{
    try
    {
        StringDictionary sd = new StringDictionary();
        sd.Add("Bob", "My name is Bob");
        sd.Add("joe", "My name is joe");
        sd.Add("bob", "My name is bob"); // << throws an exception because
                                         //    "bob" is already a key!
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

나는이 차이에 더 많은 관심을 끌기 위해이 답변을 추가 하고 있는데, 이는 IMO가 현대 대 구식 차이보다 더 중요합니다.


StringDictionary .NET 1.1에서 제공되며 IEnumerable

Dictionary<string, string> .NET 2.0에서 제공되며 IDictionary<TKey, TValue>,IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable

IgnoreCase는 키 입력에만 설정됩니다. StringDictionary

Dictionary<string, string> LINQ에 좋다

        Dictionary<string, string> dictionary = new Dictionary<string, string>();
        dictionary.Add("ITEM-1", "VALUE-1");
        var item1 = dictionary["item-1"];       // throws KeyNotFoundException
        var itemEmpty = dictionary["item-9"];   // throws KeyNotFoundException

        StringDictionary stringDictionary = new StringDictionary();
        stringDictionary.Add("ITEM-1", "VALUE-1");
        var item1String = stringDictionary["item-1"];     //return "VALUE-1"
        var itemEmptystring = stringDictionary["item-9"]; //return null

        bool isKey = stringDictionary.ContainsValue("VALUE-1"); //return true
        bool isValue = stringDictionary.ContainsValue("value-1"); //return false

좀 더 "현대적인"클래스 인 것 외에도 Dictionary가 StringDictionary보다 메모리 효율성이 크다는 것을 알았습니다.


또 다른 관련 포인트는 (내가 틀렸다면 정정하십시오) System.Collections.Generic.Dictionary는 응용 프로그램 설정 ( Properties.Settings) 에서 사용할 수 없다는 것 System.Collections.Specialized.StringDictionary입니다.

참고 URL : https://stackoverflow.com/questions/627716/stringdictionary-vs-dictionarystring-string

반응형