Joe
Joe

Reputation: 6806

What C# feature is this?

In my C# application (which uses C# 9) I had a line of code like this:

var filterCallback = new HitTestFilterCallback(
    el => el is Path s && s.DataContext is IShape shp ?
        HitTestFilterBehavior.Continue : HitTestFilterBehavior.ContinueSkipSelf);

Resharper suggested I "merge sequential checks". It rewrote the code to this

        var filterCallback = new HitTestFilterCallback(
            el => el is Path {DataContext: IShape shp}
                ?
                HitTestFilterBehavior.Continue : HitTestFilterBehavior.ContinueSkipSelf);

I am interested in the bit that lets me write

{DataContext: IShape shp} 

in place of

s.DataContext is IShape shp

Is there a specific technical language name for this feature? I want to read up on it to understand it better but I don't even know what to call it. I tried looking under "pattern matching" and reading about the "is" keyword but don't see anything that looks like it?

Upvotes: 3

Views: 110

Answers (1)

Yair Halberstadt
Yair Halberstadt

Reputation: 6831

The feature is called "recursive patterns". In particular it is using a "property pattern" as part of a "recursive pattern".

You can find the proposal document here: https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/patterns.md

You can find a more informal blog post about it here: https://devblogs.microsoft.com/dotnet/do-more-with-patterns-in-c-8-0/

Upvotes: 1

Related Questions