2012-08-28 22 views
8

Belgeleri takip ederek denedim ve çalışamıyorum. Anahtar dizesi ile KeyedCollection var.KeyedCollection Dize Kasası Duyarsızlık

Dize anahtar büyük/küçük harf duyarlı bir KeyedCollection içinde nasıl yapılır?

Bir sözlükte yalnızca StringComparer.OrdinalIgnoreCase'i ctor'a geçirebilirsiniz. Eğer tip WordDefKeyed harf duyarsız varsayılan olarak almasını istiyorsanız

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 

cevap

7

, sonra varsayılan, parametresiz oluşturucu şöyle, bunun için ayrı bir IEqualityComparer<string> örneğini geçmelidir:

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

StringComparer class bazı varsayılan vardır yaygın verilerin tipine bağlı olarak kullanılır IEqualityComparer<T> uygulamaları Eğer depolamak:

geçerli kültürdür olandan kültür diğer için StringComparer gerekiyorsa, o zaman statik Create method belirli CultureInfo için StringComparer oluşturmak için çağırabilir.