Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accept only certain strings for a method in C#

I've created a method in C# as you can see below

public static IEnumerable<User> QueryTheAD(string filter, string identifier) {
  if ( filter == "ANiceString" ) {
    // sexy code here
  }
}

which works well. However VS rightly shows that not all code path returns a value.

So is it therefore possible for me to specify that filter can only be one of these:

  • "Tutor"
  • "Year"
  • "SecondName"
like image 449
Jake Hendy Avatar asked Jul 09 '26 10:07

Jake Hendy


1 Answers

Maybe you should use an enum instead of string? :)

enum filterEnum
{
   Tutor,
   Year,
   SecondName
}

public static IEnumerable<User> QueryTheAD(filterEnum filter, string identifier) 

Enjoy ;)

like image 122
animaonline Avatar answered Jul 11 '26 22:07

animaonline