9

ドキュメントに従ってみましたが、動作させることができません。キー文字列を含むKeyedCollectionがあります。

KeyedCollectionで文字列キーの大文字と小文字を区別しないようにするにはどうすればよいですか?

辞書では、ctorでStringComparer.OrdinalIgnoreCaseを渡すことができます。

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
4

1 に答える 1

9

タイプWordDefKeyedをデフォルトで大文字と小文字を区別しないようにする場合は、次のように、デフォルトのパラメーターなしのコンストラクターがIEqualityComparer<string>インスタンスを渡す必要があります。

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

このStringComparerクラスIEqualityComparer<T>には、格納しているデータのタイプに応じて一般的に使用されるデフォルトの実装がいくつかあります。

現在のカルチャ以外StringComparerのカルチャのが必要な場合は、静的メソッドを呼び出して特定ののを作成できます。CreateStringComparerCultureInfo

于 2012-08-28T03:27:37.623 に答える