c# - 如何限制具有特定名称的字符串的值
问题描述
我想要一个限制为 4-5 个值的ex: "Insert", "update", "delete", "check"
字符串(这些值之一是这样的:true
false
bool
//not what i want
switch(str){
case "update":
isgoodvul=true;
break;
case "delete":
//(and so on).....
}
但更像
//what i want
string limited="hello world";
//error;
string limited="update";
//not error;
string any_other_string_name_exept_limited="hello world";
//not error;
string any_other_string_name_exept_limited="update";
//not error;
解决方案
您可以使用枚举来获得您想要的行为。定义这个:
enum MyAction
{
insert = 0,
update =1,
delete = 2,
check = 3
}
然后像这样使用它:
Enum action = MyAction.update;
switch(action){
case(MyAction.insert)
//Do insert
case(MyAction.update)
//Do update
...
}
推荐阅读
- python - pandas 将 multiindex 重新索引到更高频率的日期
- raspberry-pi3 - 如果 ATtiny25 上的 0x200 之后的主函数地址,Avr C 代码不起作用
- python - dataframe.resample('4T').mean() 产生不正确的第一个结果,但后续结果是一致的
- mysql - 如何打印一个 MySQL 表来计算每个前三个驱动程序的驱动器数量?
- java - JPA CriteriaBuilder 方法:键和值。如何使用它们?
- c# - 如何从 App.xaml.cs 或同一解决方案中的外部后台服务项目调用视图模型中的方法
- linux - 使用内置 printf 以毫秒或纳秒为单位打印当前时间
- asp.net-mvc - Orchard Core主题视图中液体和cshtml页面之间的区别
- aws-api-gateway - AWS API Gateway 的安全性
- c# - 访问 C# 的数组数组