in

CodePrairie .NET

South Dakota .NET User Group

chrisortman

Something interesting about XmlNodeList

Here's something odd that I noticed about XmlNodeList today. Consider this example:

                string xml = "texttext2";
		XmlDocument doc = new XmlDocument();
		doc.LoadXml(xml);
		XmlNodeList list = doc.SelectNodes("//item");
		Console.WriteLine(list.Count.ToString()); // count = 2
		XmlNode n = list[list.Count+100];
		if(n == null) {
			Console.WriteLine("No IndexOutOfRangeException");
		} else {
			Console.WriteLine("How did i get here?");
		}
		Console.ReadLine();

Now I would expect this to throw an IndexOutOfRangeException wouldn't you? But, it doesn't. Instead it returns null. Granted I ran into this because of my own silly error, but it was slightly less intuitive to track down the error based on the exception that got thrown.

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add
Powered by Community Server (Commercial Edition), by Telligent Systems