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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:56:14+00:00 2026-06-01T19:56:14+00:00

I want to be able to call properties on objects that might be null

  • 0

I want to be able to call properties on objects that might be null but not explicitly have to check whether they are null or not when calling.

Like this:

var something = someObjectThatMightBeNull.Property;

My idea is to create a method that takes an Expression, something like this:

var something = GetValueSafe(() => someObjectThatMightBeNull.Property);

TResult? GetValueSafe<TResult>(Expression<Func<TResult>> expression) 
    where TResult : struct
{
    // what must I do?
}

What I need to do is to inspect the expression and determine if someObjectThatMightBeNull is null or not. How would I do this?

If there is any smarter way of being lazy I’d appreciate that too.

Thanks!

  • 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-06-01T19:56:17+00:00Added an answer on June 1, 2026 at 7:56 pm

    It’s complicated, but it can be done, without leaving “expression-land”:

    // Get the initial property expression from the left 
    // side of the initial lambda. (someObjectThatMightBeNull.Property)
    var propertyCall = (MemberExpression)expression.Body;
    
    // Next, remove the property, by calling the Expression 
    // property from the MemberExpression (someObjectThatMightBeNull)
    var initialObjectExpression = propertyCall.Expression;
    
    // Next, create a null constant expression, which will 
    // be used to compare against the initialObjectExpression (null)
    var nullExpression = Expression.Constant(null, initialObjectExpression.Type);
    
    // Next, create an expression comparing the two: 
    // (someObjectThatMightBeNull == null)
    var equalityCheck = Expression.Equal(initialObjectExpression, nullExpression);
    
    // Next, create a lambda expression, so the equalityCheck 
    // can actually be called ( () => someObjectThatMightBeNull == null )
    var newLambda = Expression.Lambda<Func<bool>>(equalityCheck, null);
    
    // Compile the expression. 
    var function = newLambda.Compile();
    
    // Run the compiled delegate. 
    var isNull = function();
    

    That being said, as Andras Zoltan has so eloquently put in the comments: “Just because you can doesn’t mean you should.” Make sure you have a good reason to do this. If there’s a better way to, then do that instead. Andras has a great workaround.

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

Sidebar

Related Questions

I want my app to be able to tell whether a call was missed,
I want to be able to call a method that creates an object and
I have an object with a number of properties. I want to be able
I have a method call that returns Type . I want to make use
Summary I want to be able to call a JavaScript function from a Flex
I want to be able to make an HTTP call updating some select boxes
I want to stop outgoing call and open the application. The app is able
Want to be able to provide a search interface for a collection of objects
I want to be able to capture the exception that is thrown when a
I have a collection of objects which have several properties in each of them.

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.