Reputation: 155
I am creating static class using a static method that is comparing a string filled with selections from a user input, and a predefined array of what is "supposed to be the inputs";
My concern is the placement of the pre-defined array within the class and if the correct data type to be used is actually an array or a dictionary.
I will have roughly 150 strings max in the pre-defined and ready to compare against string array.
here is what I have so far.
public static class SelectionMatchHelper
{
static readonly string[] predefinedStrings = {"No answer", "Black", "Blonde"};
public readonly static bool SelectionMatch(string[] stringsWeAreComparingAgainst, int validCount)
{
int numberOfMatches = 0;
for (int x = 0; x < "length of string array"; x++)
{
//will loop through and check if the value exists because the array to match against will not always have the same index length
numberOfMatches += 1;
}
numberOfMatches.Dump();
if (numberOfMatches == 0 || numberOfMatches < validCount || numberOfMatches > validCount) return false;
return true;
}
}
what this basically does, is based on the number of parameters the user must fulfill, the method gets the matches, if it doesn't equal that amount then it returns false. The input the user is using is a drop down, so this is only being used to make sure my values aren't tampered with prior to saving.
My question is what data type is best used for this scenario an string array/list or dictionary? The second question is where should that be placed in order to avoid a thread issue, inside the method or out?
EDIT - I just want to add that the pre-defined values would remain the same, so I would end up making that field a readonly const value.
EDIT 2 - Just re-checked my code I wont be using CompareOrdinal because I totally forgot the part where the order matters. So it will be a key look up. So I will remove the inside of the method so people don't confused. The main question is still the same.
Thanks for your help everyone.
Upvotes: 2
Views: 1379
Reputation: 100630
From readability point of view HashSet
is the best type as it specifically exists for "item is present in the set".
static readonly HashSet<string> predefinedStrings = new HashSet<string>(
new []{"No answer", "Black", "Blonde"},
StringComparer.Ordinal);
if (predefinedStrings.Contains("bob"))....
Fortunately HashSet
also thread safe for read-only operations, provides O(1) check time and supports case insensitive comparison if you need one.
Upvotes: 3