C#: Add some examples with the extended property pattern syntax.

This commit is contained in:
Michael Nebel
2022-01-25 13:23:30 +01:00
parent 83e7fae578
commit 833e8e4f1d
2 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
using System;
public record Point(int X, int Y);
public record Line(Point P1, Point P2);
public record Wrap(Line L);
public class PropertyPatterns
{
private static Line l = new Line(new Point(1, 2), new Point(3, 6));
private static Wrap w = new Wrap(l);
public void M1()
{
if (l is { P1: { X: 1 } })
{
}
if (l is { P1.X: 2 })
{
}
}
public void M2()
{
if (w is { L: { P2: { Y: 3 } } })
{
}
if (w is { L.P2.Y: 4 })
{
}
}
public void M3()
{
if (w is { L: { P2.Y: 5 } })
{
}
if (w is { L.P2: { Y: 6 } })
{
}
}
public void M4()
{
if (l is { P1: { X: 7 }, P1: { Y: 8 } })
{
}
if (l is { P1.X: 9, P1.Y: 10 })
{
}
}
}

View File

@@ -1,4 +1,7 @@
recordTypes
| PropertyPatterns.cs:3:1:3:34 | Point |
| PropertyPatterns.cs:4:1:4:39 | Line |
| PropertyPatterns.cs:5:1:5:27 | Wrap |
| RecordTypes.cs:3:1:6:2 | MyEntry |
| RecordTypes.cs:8:1:8:53 | MyClassRecord |
| RecordTypes.cs:10:1:10:70 | MyReadonlyRecordStruct |
@@ -9,5 +12,8 @@ recordStructs
| RecordTypes.cs:12:1:12:51 | MyRecordStruct1 |
| WithExpression.cs:9:1:9:47 | MyRecordStruct2 |
recordClass
| PropertyPatterns.cs:3:1:3:34 | Point |
| PropertyPatterns.cs:4:1:4:39 | Line |
| PropertyPatterns.cs:5:1:5:27 | Wrap |
| RecordTypes.cs:3:1:6:2 | MyEntry |
| RecordTypes.cs:8:1:8:53 | MyClassRecord |