Skip to content

Instantly share code, notes, and snippets.

@mgroves
Created February 13, 2012 20:11
Show Gist options
  • Select an option

  • Save mgroves/1819784 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/1819784 to your computer and use it in GitHub Desktop.
// better like this?
var isMyFavoriteLetter = new List<string> { "A", "B", "C"}.Contains(myFavoriteLetter);
// or better like this?
var isMyFavoriteLetter = myFavoriteLetter == "A" || myFavoriteLetter == "B" || myFavoriteLetter == "C";
@mattsonlyattack
Copy link
Copy Markdown

A list and using contains seems to match my mental model of the question.

@cyberzed
Copy link
Copy Markdown

or

var isMyFavoriteLetter = new []{ "A", "B", "C"}.Contains(myFavoriteLetter);

@spetryjohnson
Copy link
Copy Markdown

I prefer:

myFavoriteLetter.IsIn("A", "B", "C").

IsIn() is easy to write:

public static bool IsIn(this T p_value, params T[] p_list) {
return p_value.IsIn(p_list.ToList());
}

public static bool IsIn(this T p_value, IList p_list) {
if (p_list == null || p_list.Count == 0)
return false;

return p_list.Contains(p_value);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment