而不是使用如下代碼所示的Switch語句,是否有其他方法來檢查foo.Type
是否與Parent.Child
類中的任何常量匹配?如何通過靜態類常量進行循環?
預期的目標是循環遍歷所有常量值以查看foo.Type
是否匹配,而不必將每個常量指定爲case
。
父類:
public class Parent
{
public static class Child
{
public const string JOHN = "John";
public const string MARY = "Mary";
public const string JANE = "Jane";
}
}
代碼:
switch (foo.Type)
{
case Parent.Child.JOHN:
case Parent.Child.MARY:
case Parent.Child.JANE:
// Do Something
break;
}
可以定義陣列中的全部通過數組常量和環路檢查常數值? – Peter
您可以將所有常量值放入ArrayList中,在比較特定元素是否等於foo.Type時迭代列表。 – HaroldSer
這可能有助於http://stackoverflow.com/questions/14971631/convert-an-enum-to-liststring,然後只是做一個** IndexOf(foo.Type)** –