I suggest you ...

Add "?." operator to C#

Add the "?." operator to C# and make it chainable like CoffeeScript so we don't have to write null reference checks for every nested property. An example would be:
myObject?.Items?.[0]?.DoSomething()

5,426 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Jamshid Asadzadeh shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    The “?.” operator is now implemented in the latest CTP of Visual Studio “14”.

    For links to details about this and other new C# language features, check out this CodePlex post:

    https://roslyn.codeplex.com/discussions/552378

    Please try out the feature, and see how it works for you!

    Thanks again for great input, and the many many votes!

    Mads Torgersen (MSFT)
    C# Language PM

    186 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • gzak commented  ·   ·  Flag as inappropriate

        Actually, I should add: by doing something like someObject?.Result, if someObject is not null, it will return the value of the Result property as normal, but as a Nullable<int>. Only if someObject is null does the ?. operator make a difference by returning a null of type Nullable<int>. This is where the ?? operator comes in, to clean up the returned Nullable<int> and turn it back into a regular int (so that you can use it with existing code that expects the usual int rather than Nullable<int>).

      • gzak commented  ·   ·  Flag as inappropriate

        Pavel,

        Not quite: my intent was to simplify the null check of accessed objects along the way to the destination property, not just to clean up the final property value. In particular, suppose in my example someObject was null, and the Result property on that object was of type int (not Nullable<int>). What would someObject?.Result return? Obviously null of type Nullable<int>, but if I actually wanted to preserve the original type of the property (int) by providing some default value, I was pointing out that you can simply tack on ?? 0 using the existing null coalescing operator to do so.

        Basically, something like a ?. null-checked property accessor operator (what would it do if the right hand side was a method, or a side-effect-full property getter? Not sure, but it's an interesting edge case to consider...) coupled with the existing ?? null coalescing operator would go very far in alleviating developers from gratuitous null-checks throughout the code.

        Though that's an interesting example of automatic null-checking in LINQ2SQL, I didn't know that. Is it still true in Entity Framework? Also, I know from experience that this type of automatic null-checking is /not/ supported by LINQ2XML. But by providing a ?. operator, we would have a general way of handling such null checking anywhere in the code, not just in LINQ.

      • Lauri Lüüs commented  ·   ·  Flag as inappropriate

        This is really needed feature. I have created following lambda expression, it's not so pretty but does it's work
        public static V Nz<T, V>(this T a, Func<T, V> f, V defaultValue)
        {
        try
        {
        return f(a);
        }
        catch
        {
        return defaultValue;
        }
        }

        so now I can do things like var city = customer.Nz(x=>x.Contact.Address.City.Name, "");
        it's not customer?.Contact?.Address?.City?.Name but works quite well

      • Pavel commented  ·   ·  Flag as inappropriate

        Hey, you actually CAN do this in LINQ-to-SQL. For example:

        var query =
        from user in dataContext.Users
        select new
        {
        Name = user.Name,
        CategoryName = user.Category.Name
        };
        var users = query.ToList();

        turns into the following sql:

        select u.name, c.name
        from users u
        left join category c on c.id = u.categoryid

        so it outputs null as categoryname for users not having categories, and the code executes just fine.

        for value types, you have to make a trick though:

        var query =
        from user in dataContext.Users
        select new
        {
        Name = user.Name,
        CategoryCreatedOn = (DateTime?)user.Category.CreatedOn
        };

        in order to allow nulls linq2sql gets from the DB be bound to the model correctly.

        You can't do this with in-memory objects, though.

      • gzak commented  ·   ·  Flag as inappropriate

        Absolutely. And coupled with the already existing "??" operator, you can salvage Nullable<> types back to value types:

        int result = someObject?.Result ?? 0;

      1 2 6 7 8 10 Next →

      Feedback and Knowledge Base