What's the benefit of var patterns in C#7?

后端 未结 4 802
情深已故
情深已故 2020-12-25 11:10

I don\'t understand the use case of var patterns in C#7. MSDN:

A pattern match with the var pattern always succeeds. Its syn

4条回答
  •  感动是毒
    2020-12-25 11:55

    Only thing I can think of offhand is if you find that you've written two identical blocks of code (in say a single switch), one for expr is object a and the other for expr is null.

    You can combine the blocks by switching to expr is var a.

    It may also be useful in code generation scenarios where, for whatever reason, you've already written yourself into a corner and always expect to generate a pattern match but now want to issue a "match all" pattern.

提交回复
热议问题