There are many cases in which you want to retrieve a SPList object. In some cases you can’t just assume the list exists so before referencing it, you’d want to check this first. Within the WSS v3 object model there was no neat way to check for the existence of a list. Indexing into a SPListCollection based on for example the name of a non-existing list resulted in a System.NullReferenceException. For this reason, it was quite common to check for the existence of a SPList whitin a try-catch block.
In SharePoint 2010’s object model the SPListCollection Class has a new method called “TryGetList”. This method does exactly what the name suspects. Within the SPListCollection is being searched to a SPList with the specified name. If the list does not exist, a null reference is returned
SPSite currentSite = SPContext.Current.Site;
SPList mySPList = currentSite.RootWeb.Lists.TryGetList("VoorbeeldLijst");
if (mySPList != null)
{
...
...
}
