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

  • Home
  • SEARCH
  • 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 58285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:49:27+00:00 2026-05-10T17:49:27+00:00

C#6 Update In C#6 ?. is now a language feature : // C#1-5 propertyValue1

  • 0

C#6 Update

In C#6 ?. is now a language feature:

// C#1-5 propertyValue1 = myObject != null ? myObject.StringProperty : null;   // C#6 propertyValue1 = myObject?.StringProperty; 

The question below still applies to older versions, but if developing a new application using the new ?. operator is far better practice.

Original Question:

I regularly want to access properties on possibly null objects:

string propertyValue1 = null; if( myObject1 != null )     propertyValue1 = myObject1.StringProperty;  int propertyValue2 = 0; if( myObject2 != null )     propertyValue2 = myObject2.IntProperty; 

And so on…

I use this so often that I have a snippet for it.

You can shorten this to some extent with an inline if:

propertyValue1 = myObject != null ? myObject.StringProperty : null; 

However this is a little clunky, especially if setting lots of properties or if more than one level can be null, for instance:

propertyValue1 = myObject != null ?      (myObject.ObjectProp != null ? myObject.ObjectProp.StringProperty) : null : null; 

What I really want is ?? style syntax, which works great for directly null types:

int? i = SomeFunctionWhichMightReturnNull(); propertyValue2 = i ?? 0; 

So I came up with the following:

public static TResult IfNotNull<T, TResult>( this T input, Func<T, TResult> action, TResult valueIfNull )     where T : class {     if ( input != null ) return action( input );     else return valueIfNull; }  //lets us have a null default if the type is nullable public static TResult IfNotNull<T, TResult>( this T input, Func<T, TResult> action )     where T : class     where TResult : class { return input.IfNotNull( action, null ); } 

This lets me us this syntax:

propertyValue1 = myObject1.IfNotNull( x => x.StringProperty ); propertyValue2 = myObject2.IfNotNull( x => x.IntProperty, 0);  //or one with multiple levels propertyValue1 = myObject.IfNotNull(      o => o.ObjectProp.IfNotNull( p => p.StringProperty ) ); 

This simplifies these calls, but I’m not sure about checking this sort of extension method in – it does make the code a little easier to read, but at the cost of extending object. This would appear on everything, although I could put it in a specifically referenced namespace.

This example is a rather simple one, a slightly more complex one would be comparing two nullable object properties:

if( ( obj1 == null && obj2 == null ) ||      ( obj1 != null && obj2 != null && obj1.Property == obj2.Property ) )     ...  //becomes if( obj1.NullCompare( obj2, (x,y) => x.Property == y.Property )      ... 

What are the pitfalls of using extensions in this way? Are other coders likely to be confused? Is this just abuse of extensions?


I guess what I really want here is a compiler/language extension:

propertyValue1 = myObject != null ? myObject.StringProperty : null;  //becomes propertyValue1 = myObject?StringProperty; 

This would make the complex case far easier:

propertyValue1 = myObject != null ?      (myObject.ObjectProp != null ? myObject.ObjectProp.StringProperty) : null  //becomes propertyValue1 = myObject?ObjectProp?StringProperty; 

This would only work for value types, but you could return nullable equivalents:

int? propertyValue2 = myObject?ObjectProp?IntProperty;  //or  int propertyValue3 = myObject?ObjectProp?IntProperty ?? 0; 
  • 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-10T17:49:27+00:00Added an answer on May 10, 2026 at 5:49 pm

    We independently came up with the exact same extension method name and implementation: Null-propagating extension method. So we don’t think it’s confusing or an abuse of extension methods.

    I would write your ‘multiple levels’ example with chaining as follows:

    propertyValue1 = myObject.IfNotNull(o => o.ObjectProp).IfNotNull(p => p.StringProperty); 

    There’s a now-closed bug on Microsoft Connect that proposed ‘?.’ as a new C# operator that would perform this null propagation. Mads Torgersen (from the C# language team) briefly explained why they won’t implement it.

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

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 79k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Try this. May 11, 2026 at 3:58 pm
  • added an answer You should use an inline tag like <span> May 11, 2026 at 3:58 pm
  • added an answer Does it have to be done within vim? Could you… May 11, 2026 at 3:58 pm

Related Questions

I was looking for a generic method in .Net to encode a string for
FYI - I am a mainframer that moved to the .NET world a couple
I have an application written in Delphi 7 which does not require an admin
I have some code that effectively does this : File file = new File(C:\\Program

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.