2012-08-28 27 views
8

प्रलेखन का पालन करने का प्रयास किया और मैं इसे काम नहीं कर सकता। कुंजी स्ट्रिंग के साथ एक keyedCollection है।कीड कोलेक्शन स्ट्रिंग केस असंवेदनशील

कुंजीडॉल चयन में स्ट्रिंग कुंजी केस असंवेदनशील कैसे बनाएं?

एक डिक्शनरी पर बस स्ट्रिंग कॉम्पारेर को पार कर सकता है। ऑर्डिनल इग्नोरकेस सीटीआर में।

private static WordDefKeyed wordDefKeyed = new WordDefKeyed(StringComparer.OrdinalIgnoreCase); // this fails 

public class WordDefKeyed : KeyedCollection<string, WordDef> 
{ 
     // The parameterless constructor of the base class creates a 
     // KeyedCollection with an internal dictionary. For this code 
     // example, no other constructors are exposed. 
     // 
     public WordDefKeyed() : base() { } 

     public WordDefKeyed(IEqualityComparer<string> comparer) 
      : base(comparer) 
     { 
      // what do I do here??????? 
     } 

     // This is the only method that absolutely must be overridden, 
     // because without it the KeyedCollection cannot extract the 
     // keys from the items. The input parameter type is the 
     // second generic type argument, in this case OrderItem, and 
     // the return value type is the first generic type argument, 
     // in this case int. 
     // 
     protected override string GetKeyForItem(WordDef item) 
     { 
      // In this example, the key is the part number. 
      return item.Word; 
     } 
} 

private static Dictionary<string, int> stemDef = new Dictionary<string, int(StringComparer.OrdinalIgnoreCase); // this works this is what I want for KeyedCollection 

उत्तर

7

आप अपने प्रकार WordDefKeyed केस-संवेदी डिफ़ॉल्ट रूप से होना चाहते हैं, फिर अपने डिफ़ॉल्ट, parameterless निर्माता एक IEqualityComparer<string> उदाहरण यह करने के लिए है, तो तरह से पारित करना चाहिए:

public WordDefKeyed() : base(StringComparer.OrdinalIgnoreCase) { } 

StringComparer class कुछ डिफ़ॉल्ट है IEqualityComparer<T> कार्यान्वयन जो सामान्य रूप से डेटा के प्रकार पर निर्भर करते थे कर रहे हैं आप भंडारण कर रहे हैं:

  • StringComparer.Ordinal और StringComparer.OrdinalIgnoreCase - जब आप मशीन-पठनीय तारों का उपयोग कर रहे हों, तो स्ट्रिंग जो उपयोगकर्ता में दर्ज या प्रदर्शित नहीं होती हैं।

  • StringComparer.InvariantCulture और StringComparer.CultureInvariantIgnoreCase - जब आप स्ट्रिंग का उपयोग कर रहे हों तो यूआई को नहीं दिखाया जाएगा, लेकिन संस्कृति के प्रति संवेदनशील हैं और संस्कृतियों में समान हो सकते हैं।

  • StringComparer.CurrentCulture और StringComparer.CurrentCultureIgnoreCase - मौजूदा संस्कृति के लिए विशिष्ट तारों के लिए उपयोग करें, जैसे कि जब आप उपयोगकर्ता इनपुट एकत्र कर रहे हों।

आप एक संस्कृति अन्य एक है कि वर्तमान संस्कृति है की तुलना के लिए एक StringComparer की जरूरत है, तो आप कॉल कर सकते हैं स्थिर Create method एक विशिष्ट CultureInfo के लिए एक StringComparer बनाने के लिए।

संबंधित मुद्दे