3

I would like to pass an argument to a method.

public void Foo(string range)
{
} 

How can I restrict or limit the string passed to this method? Like only High/Medium/Low can be chosen when call Foo, not any other string?

1 Answer 1

6

You can pass as enum, instead of string.

public void Foo(Range range)
{
}


enum Range {
    High,
    Medium,
    Low
}
1
  • 1
    thanks for the quick reply. never used enum before. just tried your method above and worked great. I just have to add .toString() to convert the value to string format
    – ShawnL
    Commented Oct 2, 2022 at 21:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.