我正在构建一个有趣的小应用程序,以确定我是否应该自行车上班.
我想测试看它是下雨还是雷雨(ing).
public enum WeatherType : byte
{ Sunny = 0, Cloudy = 1, Thunderstorm = 2, Raining = 4, Snowing = 8, MostlyCloudy = 16 }
Run Code Online (Sandbox Code Playgroud)
我以为我可以这样做:
WeatherType _badWeatherTypes = WeatherType.Thunderstorm | WeatherType.Raining;
if(currentWeather.Type == _badWeatherTypes)
{
return false;//don't bike
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为_badWeatherTypes是两种类型的组合.我想将它们分开,因为这应该是一种学习经验,并且将它分开可能在其他情况下有用(IE,Invoice not paid reason's etc ...).
我也不愿意这样做(这会删除为多人配置的能力)
if(WeatherType.Thunderstorm)
{
return false; //don't bike
}
etc...
Run Code Online (Sandbox Code Playgroud) 我认为Commodore 128 Basic中有一个in运算符.
c#中是否有in运算符?
我的意思是有一个善良的经营者
if(aString in ["a", "b", "c"])
Console.Out.WriteLine("aString is 'a', 'b' or 'c'");
Run Code Online (Sandbox Code Playgroud)
Edit1:目前我需要它来决定枚举值是否在某些枚举值的范围内.
Edit2:谢谢大家对Contains()的解决方案.我将来会用它.但目前我需要枚举值.我可以用Contains()或其他方法替换以下语句吗?
public enum MyEnum { A, B, C }
class MyEnumHelper
{
void IsSpecialSet(MyEnum e)
{
return e in [MyEnum.A, MyEnum.C]
}
}
Run Code Online (Sandbox Code Playgroud)
编辑3:对不起,这不是基本的.我只是用Google搜索了一段时间,发现Turbo Pascal是我能看到它的候选人.见http://en.wikipedia.org/wiki/Pascal_%28programming_language%29#Set_types
Edit4:到目前为止的最佳答案(2012年2月15日结束):