Is there a better way to write new List {“a”, “b”}.Contains(str)?

You could write an extension method: public static bool In(this T obj, params T candidates) { return obj. In((IEnumerable)candidates); } public static bool In(this T obj, IEnumerable candidates) { if(obj == null) throw new ArgumentNullException("obj"); return (candidates? Enumerable.Empty()).

Contains(obj); } Which you could then use to do: if(str. In("A", "B", "C")) { ... }.

An interesting approach. Right now I like Fredrik's second solution better, but I will keep your idea in mind. +1 – mafutrct Aug 27 '09 at 9:16 Decided to accept this answer anyway, as it is more universal.

– mafutrct Sep 7 '09 at 11:12.

I guess you could shorten it down to: if ((new { "A", "B", "C" }). Contains (str)) { Don't know how much of an actual difference it would make though. Update: if you know that you will be testing for exactly one letter, I see no reason to make a list or array of it: if ("ABC".

Contains(str)) { That code is both shorter and faster. But then again I guess that the single-letter strings were merely samples...

That's already a big improvement. I guess it is a bit faster as well. – mafutrct Aug 27 '09 at 8:11 @mafutrct: I did a performance test and it seems as if my solution is actually slower.So right now it's a choice between shorter or faster code ;o) – Fredrik Mörk Aug 27 '09 at 8:17 Luckily right now I actually need to test for single characters only.

How could I possibly miss "ABC". Contains?! – mafutrct Aug 27 '09 at 9:17.

It's a bit shorter, but also less easy to read and the performance is probably worse. – mafutrct Aug 27 '09 at 8:10 s/probably/certainly/ - don't stick that inside a loop... – ijw Aug 27 '09 at 8:33 Of course the performance is worse. If it matters in the op's case is another question :) – Christian Aug 27 '09 at 8:37.

To completely change it up: switch(str){ case "A": case "B": case "C": contains = true; break; default: contains = false; break; }.

Too much code :) – mafutrct Aug 27 '09 at 9:14 Just offering an alternative :) – Noon Silk Aug 27 '09 at 9:19.

If your short list of strings are constant, you should use a static readonly string array. The benefit is it's easy to write and it does not instantiate new List every time you need to perform the check. Private static readonly string Names = new string { "A", "B", "C" }; ... if (Names.

Contains(str)) { However, this solution is not scalable as the search is done in a linear fashion. Alternatively, you can define your constant array in a sorted manner and use BinarySearch over the array. // this has to be sorted private static readonly string Names = new string { "A", "B", "C" }; ... if (Array.

BinarySearch(Names, str) >= 0) {.

From a performance POV, I like your solution. However, my main concern was code length. – mafutrct Aug 27 '09 at 9:12.

I want to test whether a certain string is contained in a short list of strings. However, this seems bloated. For instance, iirc, in Java I could simply write {"A", "B", "C"}.

Contains(str) which would be much preferable to the above. I'm sure there is a better way in C#. Could you point it out?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions