Skip to content

Use pattern matching in the comparers #4065

Closed

Description

@stevenaw

Most of the comparers perform type-checking using the following pattern:

if (x is DesiredType)
  ((DesiredType)x).DoSomething()

Newer versions of C# added the ability to avoid the second cast by defining a temp variable.

if (x is DesiredType dt)
  dt.DoSomething()

This helps simplify code and provides a small perf boost by avoiding an unneccessary cast. This could also minimize future changes if the comparers ever get a generic overload as pattern matching can play nicer than casts in that case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions