可能有
一些
这种方法的优点如下:
string options =
(model.Fruits.Value ? "FR," : "")
+ (model.Drinks.Value ? "DR," : "")
+ (model.Lunch.Value ? "LU," : "")
+ (model.Dinner.Value ? "DI," : "");
具体来说,这将是
string.Concat
(见
example here
-你在找单曲
call string [mscorlib]System.String::Concat(string, string, string, string)
,这样就避免了大量中间字符串(
+=
)在“所有选项”场景中。但是,坦率地说,我建议使用
[Flags] enum
相反,即
[Flags]
enum FoodOptions {
None = 0,
Fruits = 1,
Drinks = 2,
Lunch = 4,
Dinner = 8,
}
var options = FoodOptions.None;
if(model.Fruits.Value) options |= FoodOptions.Fruits;
if(model.Drinks.Value) options |= FoodOptions.Drinks;
if(model.Lunch.Value) options |= FoodOptions.Lunch;
if(model.Dinner.Value) options |= FoodOptions.Dinner;
它只是整数。