我想定义一个带参数的函数,该参数必须是特定的,在列表中定义(例如)
我记得几年前这样做过,但我的记忆力已经让我失望了。
例子
public void foo(specific choice list defined in my class){}
特定列表 = x,y,z
消费它列出这个(我认为)
foo(myclass.x)
为了扩展@kaffekopp 的评论,您似乎在谈论一个enum
,即
public enum MyEnum // declare the enum type (optional: specify an underlying integer type)
{
Apple, // define the choices (note: enums are **not** strictly enforced;
Pear, // they are just named integers, and at runtime you can be surprised to find
Bicycle, // that the actual value is 42, a value you never defined)
}
与(上MyClass
):
public MyEnum SomeProp {get;set;} // define a property as being of that enum type
和:
public void Foo(MyEnum option) {...} // define a method that takes a parameter of the type
那么要么:
Foo(MyEnum.Bicycle); // passing a constant/literal
或者:
MyClass obj = ...
Foo(obj.SomeProp); // passing an existing value from somewhere
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句