मैं एक तरीका है जिसके एक .NET गणन में सबसे अधिक मूल्य निर्धारित करता है लिख रहा हूँ तो मैं प्रत्येक enum मूल्य के लिए एक बिट के साथ एक BitArray बना सकते हैं:एक गणन में सबसे अधिक मूल्य ढूँढना
pressedKeys = new BitArray(highestValueInEnum<Keys>());
मैं इस की जरूरत है []() int करने के लिए, EnumType के लिए नहीं []
/// <summary>Returns the highest value encountered in an enumeration</summary>
/// <typeparam name="EnumType">
/// Enumeration of which the highest value will be returned
/// </typeparam>
/// <returns>The highest value in the enumeration</returns>
private static int highestValueInEnum<EnumType>() {
int[] values = (int[])Enum.GetValues(typeof(EnumType));
int highestValue = values[0];
for(int index = 0; index < values.Length; ++index) {
if(values[index] > highestValue) {
highestValue = values[index];
}
}
return highestValue;
}
आप देख सकते हैं, मैं Enum.GetValues के रिटर्न मान कास्टिंग हूँ: दो अलग-अलग enumerations पर, तो मैं यह एक सामान्य विधि में बदल गया। ऐसा इसलिए है क्योंकि मैं बाद में intum करने के लिए EnumType (जो एक सामान्य प्रकार पैरामीटर है) नहीं डाला जा सकता है।
कोड काम करता है। लेकिन क्या यह मान्य है? क्या मैं हमेशा Enum.GetValues () से int [] में वापसी कास्ट कर सकता हूं?
संभावित enlicate [enum के अधिकतम मूल्य प्राप्त करना] (https://stackoverflow.com/questions/203377/getting-the-max-value-of-an-enum) – palswim