Your variable naming conventions are confusing. It's customary to name local variables in lowercase (
words1
, notWords1
), and it's very confusing to have variables calledWords1
andWord1
, both of which being lists of words. I would name them more explicitly -allWordsInBook1
, for instance. Similarly,Inf
andInfs
are almost identical and very confusion.posInBook1
andposInBook2
might be clearer.I'd suggest not defining your variables at the top of the method, but closer to where you use them. In your code, when you start using
Index1
, for instance, you have to scroll back a page to remember what it is.You're not actually incrementing
Counter1
andCounter3
anywhere, are you? They're always identical toInf
andInfs
respectively? In that case, they're just adding visual noise and cognitive weight.Start1/Start2
should be namedcounter1/counter2
, since they're the ones that are incremented.Again, every time you find yourself check for equality with
ToLower
, replace it withstring1.Equals(string2, StringComparison.CurrentCultureIgnoreCase)
. When dealing with a huge number of strings inside nested loops, this can have a real effect on memory usage.In your Main method, you're reading a file, using Regex to replace whitespace with a hyphen, then splitting on the hyphen inside the method. Why not save a stage (and a copy of the whole string) by splitting on the regex directly?
Regex.Split
can do it easily:var wordsInBook1 = Regex.Split(Book1, @"\s+|-");