Class Cs.TypePattern

java.lang.Object
org.openrewrite.csharp.tree.Cs.TypePattern
All Implemented Interfaces:
Cs, Cs.Pattern, org.openrewrite.java.tree.Expression, org.openrewrite.java.tree.J, org.openrewrite.Tree
Enclosing interface:
Cs

public static final class Cs.TypePattern extends Object implements Cs.Pattern
Represents a C# type pattern, which matches a value against a type and optionally assigns it to a new variable.

For example:

     // Simple type pattern
     if (obj is string)

     // Type pattern with variable declaration
     if (obj is string str)

     // Type pattern with array type
     if (obj is int[])

     // Type pattern with generic type
     if (obj is List<string> stringList)

     // Type pattern with nullable type
     if (obj is string? nullableStr)

     // Switch expression with type pattern
     object value = someValue switch {
         string s => s.Length,
         int n => n * 2,
         _ => 0
     };
 
  • Constructor Details

    • TypePattern

      public TypePattern()
  • Method Details

    • getType

      public org.openrewrite.java.tree.JavaType getType()
      Specified by:
      getType in interface org.openrewrite.java.tree.Expression
    • withType

      public Cs.TypePattern withType(@Nullable org.openrewrite.java.tree.JavaType type)
      Specified by:
      withType in interface org.openrewrite.java.tree.Expression
    • acceptCSharp

      public <P> org.openrewrite.java.tree.J acceptCSharp(CSharpVisitor<P> v, P p)
      Specified by:
      acceptCSharp in interface Cs
    • getCoordinates

      public org.openrewrite.java.tree.CoordinateBuilder.Expression getCoordinates()
      Specified by:
      getCoordinates in interface org.openrewrite.java.tree.Expression