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 6368437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:40:30+00:00 2026-05-25T00:40:30+00:00

I ran into this blog post today. I’ll summarize. The blogger is commenting on

  • 0

I ran into this blog post today.

I’ll summarize. The blogger is commenting on this code and saying it was ugly.

// var line1 = person.Address.Lines.Line1 ?? string.Empty;
// throws NullReferenceException: 
//    {"Object reference not set to an instance of an object."}

// The ugly alternative

var line1 = person.Address == null
    ? "n/a"
    : person.Address.Lines == null
    ? "n/a"
    : person.Address.Lines.Line1;

The blogger then goes on to write a class that allows you to replace above code with a new syntax.

var line2 = Dis.OrDat<string>(() => person.Address.Lines.Line2, "n/a");

The code for the Class Dis is as follows.

 public static class Dis
  {
    public static T OrDat<T>(Expression<Func<T>> expr, T dat)
    {
      try
      {
        var func = expr.Compile();
        var result = func.Invoke();
        return result ?? dat; //now we can coalesce
      }
      catch (NullReferenceException)
      {
        return dat;
      }
    }
  }

So the first question I have is why would the original code containing ?? have to be replaced with the accursed looking ?: code.

My second question is why use the expression tree over the Null Coalesce? The only reason I can think of it is because they are using lazy initialization, but that is not clear from the post, nor is it clear from the test code, which is in the blogpost. I’ll post that here as well.

BTW, anyone know how to create a fixed sized code block window? Or is a non scrolling codeblock prefered here? I didn’t see anything in the Meta blog.

  • 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. Editorial Team
    Editorial Team
    2026-05-25T00:40:31+00:00Added an answer on May 25, 2026 at 12:40 am

    The code is the blog is worse. A method name Dis.OrDat is ugly, and doesn’t describe what the method actually does.

    The use of an Expression<T> is redundant, it could just be:

    public static T OrDat<T>(Func<T> func, T dat)
    {
      try
      {
        return func() ?? dat;
      }
      catch (NullReferenceException)
      {
        return dat;
      }
    }
    

    He calls Compile and Invoke one after another right away, so he doesn’t actually do anything with the expression tree. Passing in the Func<T> as is would be the same, without the overhead of compiling the Func.

    But worse, the code uses exceptions for flow control, which is always bad: the person.Address property appears to be optional, so it isn’t “exceptional” for it to be null, so an exception shouldn’t be thrown by code which uses it. The catch above can’t distinguish between person.Address == null and the implementation of the Address property getter being broken internally causing an NullReferenceException to be thrown. It just swallows them all.

    So, overall, I’d be happy to disregard the blog post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into this question today and thought I should post it for the
today I ran into this problem which really bugs me, as almost the code
I ran into this code, foreach($vars as $a=>$var){ // some process here} and I
I ran into this error when building my code with CLANG: In file included
I ran into this situation today. I have an object which I'm testing for
I recently started learning 0MQ . Earlier today, I ran into a blog, Python
I ran into this statement in a piece of code: Int32 medianIndex = colorList.Count
I ran into this today and have no idea why the C# compiler isn't
I'm quite new to multithreading and today I ran into this strange problem. I
I ran into this problem today in IE6 (but is reproducible on all recent

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.