Is this pattern matching expression equivalent to not null

后端 未结 2 920
小鲜肉
小鲜肉 2021-01-22 07:33

I stumbled upon this code on github:

if (requestHeaders is {})

and I don\'t understand what it does exactly.

Upon experimenting it\'s s

相关标签:
2条回答
  • 2021-01-22 08:02

    you should be able to do like below..

    Input : str  = null         // initialize by null value
            String.IsNullOrEmpty(str)
    Output: True
    
    Input : str  = String.Empty  // initialize by empty value
            String.IsNullOrEmpty(str)
    Output: True
    

    So based on True false you can get results.

    0 讨论(0)
  • 2021-01-22 08:15

    The pattern-matching in C# supports property pattern matching. e.g.

    if (requestHeaders  is HttpRequestHeader {X is 3, Y is var y})
    

    The semantics of a property pattern is that it first tests if the input is non-null. so it allows you to write:

    if (requestHeaders is {}) // will check if object is not null
    

    You can write the same type checking in any of the following manner that will provide a Not Null Check included:

    if (s is object o) ... // o is of type object
    if (s is string x) ... // x is of type string
    if (s is {} x) ... // x is of type string
    if (s is {}) ...
    

    Read more here.

    0 讨论(0)
提交回复
热议问题