C# if-null-then-null expression

前端 未结 6 1938
误落风尘
误落风尘 2020-12-01 02:56

Just for curiosity/convenience: C# provides two cool conditional expression features I know of:

string trimmed = (input == null) ? null : input.Trim();
         


        
6条回答
  •  自闭症患者
    2020-12-01 03:04

    Something like Groovy's null-safe dereferencing operator?

    string zipCode = customer?.Address?.ZipCode;
    

    I gather that the C# team has looked at this and found that it's not as simple to design elegantly as one might expect... although I haven't heard about the details of the problems.

    I don't believe there's any such thing in the language at the moment, I'm afraid... and I haven't heard of any plans for it, although that's not to say it won't happen at some point.

    EDIT: It's now going to be part of C# 6, as the "null-conditional operator".

提交回复
热议问题