我想用参数定义一个函数,该参数必须是特定的,例如在列表中定义的
我记得这年前做过,但是我的记忆使我失败了。
示例
public void foo(specific choice list defined in my class){}
specific-list = x,y,z
将其列出(我认为)
foo(myclass.x)
答案 0 :(得分:1)
要扩展@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)
}
with(在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