You've got a few issues in that code.
1) You are doing a return inside your foreach loop
foreach (HtmlNode insidenode in htmlDocument.DocumentNode.SelectNodes("//h2"))
{
if (insidenode.InnerText.Contains("("))
{
int index = insidenode.InnerText.IndexOf("(", System.StringComparison.Ordinal);
_ListOfPlantNames.Add(insidenode.InnerText.Substring(0, index));
}
else
{
_ListOfPlantNames.Add(insidenode.InnerText);
}
return _ListOfPlantNames;
}
This would explain you getting a value of 0. Move that outside your foreach loop and...at least looking at the code you should be good to go.
However if that is not the case, then i would imagine this could be an alternative
public class Plants
{
public int PlantCount {get;set;}
public Plants()
{
ListOfPlantNames();
}
public void ListOfPlantNames()
{
List<string> _ListOfPlantNames = new List<string>();
foreach (HtmlNode insidenode in htmlDocument.DocumentNode.SelectNodes("//h2"))
{
if (insidenode.InnerText.Contains("("))
{
int index = insidenode.InnerText.IndexOf("(", System.StringComparison.Ordinal);
_ListOfPlantNames.Add(insidenode.InnerText.Substring(0, index));
}
else
{
_ListOfPlantNames.Add(insidenode.InnerText);
}
}
PlantCount = _ListOfPlantNames.Count;
}
}
Usage
Plants plant = new Plants();
plant.ListOfPlantNames();
Console.WriteLine(plant.PlantCount);
[Edit]Moved method call into constructor[/Edit]
If this still does not work then i would start to think that the issue lies somewhere in your foreach loop/conditions of the loop/conditions within the loop. But you should be able to just do list.Count either ways...just my code above provides you an alternative (a good one? who knows...but it should work). Also, if this doesn't work, could you post some of your HTML for me to look at/run my code against. That would help so i could see what is going on as i don't have access to your PC.