I have a string array in C# like so:
string[] sites = new string[] {
"http://foobar.com",
"http://asdaff.com",
"http://etc.com"
};
I’m using this array in a foreach and I would like to be able to add a “type” value of 1, 2, or 3, depending on which site I am currently iterating through. I am concatenating data with the StringBuilder from these sites. Now, I could store the site as a varchar, but it would be really neat, since this array will NEVER change to associate a number with the string and build it that way.
You can use a dictionary for this –
Dictionary<int, string>(orDictionary<string, int>).Another option is to just use a
List<string>andIndexOfto find out the index.A third option, using the static
IndexOfmethods ofArrayand not changing your array at all: