Synopsis: | Use pattern matching instead of the "as" keyword |
Language: | C# |
Severity Level: | 5 |
Category: | Object oriented |
Description: |
Since C# 7 it is possible to perform safe casting by only using the "is" operator. In previous days you had to do something like this: string text = input as string; if(text != null) { ... } It is more convenient to write this in the following way: if(input is string text) { ... } The pattern will not match if input is null and non-nullable types like int are supported as well. |
Literature References: |
Daniel Crabtree |