Is there a better/shorter way to handle (lots of) null references, for example when I’m using LinqToXML.
I wrote this extention for XElement that handles it quite nicely, but maybe there is another way?
And what about the function name? ‘And’ isn’t really descriptive.
public static class XmlExtentions { public static T And<T>(this T obj, Func<T, T> action) where T : XElement { return obj == null ? obj : action(obj); } } internal class Program { private static void Main() { //create example xml element var x = XElement.Parse('<root><find><name>my-name</name></find></root>'); //old way var test1 = x.Element('find'); if (test1 != null) test1 = test1.Element('name'); Console.WriteLine(test1); //using the extentions method var test2 = x.Element('find').And(findme => findme.Element('name')); Console.WriteLine(test2); Console.ReadLine(); } }
PS: I know I could use XPath for this example, but that’s not always the case in more complex cases.
The overall approach is reasonable – although I’m not sure about the
Func<T,T>which seems a bit restrictive. If you are limiting to returning the same thing, I wonder if just accepting the name (string) as the second arg wouldn’t be easier?Re naming – perhaps borrow from LINQ? This is essentaially a
Select– how aboutSelectOrDefault:(edit) maybe with the additional
XElementspecific: