Reputation: 2491
I have a method that takes a string and I would like to return true or false depending whether the string has been recorded previously.
So far I have it as follows:
string one = "abc";
string two = "def";
string three = "ghi";
MethodOne(string s)
{
if (s == one || s == two || s == three)
return true;
else
return false;
}
Now is there a simpler way to simplify this without using a lot of || and single string checking?
Would it better to put it in a string array and just use
if (array.contains(s))
or something similar where array is the list of strings to check against?
Upvotes: 0
Views: 124
Reputation: 8227
Would it better to put it in a string array and just use
if (array.contains(s))
or something similar where array is the list of strings to check against?
Yes, it would be better to include all the strings you need to check against the string s
into an array or a similar structure.
According to me, this is the best alternative to check the equality of your string, considering other strings because it improves a lot the readability. This form:
var arr = new[] {"abc", "def", "ghi"};
return arr.Contains(s);
is much more readable than:
if (s == one || s == two || s == three)
return true;
else
return false;
Then this operation to another programmer will be clearer.
Another reason to choose this form is that this will make the program easier to maintain in the future. What if you need to check your string against another string? You'll need only to insert another element into your array or collection, instead of checking another string in your if
statement:
if (s == one || s == two || s == three || s == four)
It's clear that this statement will be difficult to maintain and very horrible to read.
About the collection to use, I think an array is enough for your task, but there are other good alternatives, such as a List<string>
or a HashSet<string>
, as pointed out by other users.
Upvotes: 0
Reputation:
If you know values at compile time or checking is dominating operation (rather than modyfing collection) consider BinarySearch. It performs checking in O(log n) time (Contains
or Any
takes O(n) - linear):
var arr = new[] {"abc", "def", "ghi"}; // could be static or const for example
return Array.BinarySearch(arr, s) >= 0;
Remember, that input array should stay sorted.
O(log n) is important when checking big collection. Here is sample table with comparsions.
Upvotes: 0
Reputation: 101701
If you want to compare a string with few other strings, you can simplify your code using a Collection
e.g. an Array
.Then you can check whether the string you are looking for is present in the array:
return new [] { one, two, three }.Contains(s);
Another alternative would be using Any
method:
return new [] { one, two, three }.Any(x => x == s);
Ofcourse there are numerous ways to do that.But the main point is choosing the right data structure to simplify your code and keep it as simple and readable as possible.
Upvotes: 3
Reputation: 63742
You could also use a hashset if you're more concerned about speed and clarity than memory:
var hashSet = new HashSet<string> { "abc", "def", "ghi" };
hashSet.Contains("abc");
Upvotes: 2
Reputation: 48568
As you suggested best would be to create a collection and then check if value is present in it.
public bool MethodOne(string s)
{
List<string> collection = new List<string>() {"abc", "def", "ghi"};
return collection.Contains(s);
}
Upvotes: 0