他の回答で指摘されているように、おそらく辞書が良い選択でしょう。
仮定:
使用例:TKey
は、あなたが(おそらくTopic
おそらくint
)にルックアップしますタイプと一致するIDictionary<TKey, int>
の
Dictionary<int, int> occurrencesOfTopicsByTopicID = new Dictionary<int, int>();
// The following code increments the number of occurrences of a specific topic,
// identified by a variable named "idOfTopic", by one.
int occurrences;
// Try to get the current count of occurrences for this topic.
// If this topic has not occurred previously,
// then there might not be an entry in the dictionary.
if (occurrencesOfTopicsByTopicID.TryGetValue(idOfTopic, out occurrences))
{
// This topic already exists in the dictionary,
// so just update the associated occurrence count by one
occurrencesOfTopicsByTopicID[idOfTopic] = occurrences + 1;
}
else
{
// This is the first occurrence of this topic,
// so add a new entry to the dictionary with an occurrence count of one.
occurrencesOfTopicsByTopicID.Add(idOfTopic, 1);
}
あなたは一度それを持ってカウントで何かをする必要がありますか?カウントをどこかに保存する必要があるか、必要に応じて即座に計算していますか? –