Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 142399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:55:49+00:00 2026-05-11T07:55:49+00:00

Is there a better/shorter way to handle (lots of) null references, for example when

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:55:50+00:00Added an answer on May 11, 2026 at 7:55 am

    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 about SelectOrDefault:

    public static TResult SelectOrDefault<TSource, TResult>(     this TSource obj, Func<TSource, TResult> selector) where TSource : class {     return SelectOrDefault<TSource, TResult>(         obj, selector, default(TResult)); }  public static TResult SelectOrDefault<TSource, TResult>(     this TSource obj, Func<TSource, TResult> selector,     TResult @default) where TSource : class {     return obj == null ? @default : selector(obj); } 

    (edit) maybe with the additional XElement specific:

    public static XElement SelectOrDefault(     this XElement element, XName name) {     return element == null ? null : element.Element(name); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There must be a better / shorter way to do this: # Find files
Is there a better/shorter way to write the whoAmI method in the following code?
Is there a better shorter way than iterating over the array? int[] arr =
Is there a better (shorter?) way than the following? let cpucount = System.UInt16.Parse( reader.GetInt32(3).ToString()
Is there better way to enumerate all photos on the device than this one?
Is there a better way to flash a window in Java than this: public
Is there a better / cleaner / shorter way of getting the same output
Is there a better way to do this function? Even though i'm doing the
How would I make this jQuery shorter? I assume there must be a better
Is there a better way to get the same result that this produces? foreach($data['report']->result_array()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.