> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Slice Pattern

The slice pattern (`..`) is a subpattern used within C# list patterns (`[...]`) to match zero or more elements in a collection. It enables structural matching of sequences by asserting the positions of specific elements while abstracting away the length and content of the remaining sequence.

## Syntax and Mechanics

The slice pattern is denoted by two periods (`..`). It operates under strict positional evaluation rules:

1. **Single Instance Constraint:** A single list pattern can contain a maximum of one slice pattern.
2. **Zero-or-More Evaluation:** The slice pattern successfully matches even if there are zero elements available to fill the slice.
3. **Pattern Application:** The slice pattern is not limited to being a discard or a variable capture. It can be followed by *any* pattern (such as a property pattern, a list pattern, or a var pattern) to validate the shape, length, or contents of the matched sub-sequence.

## Syntax Visualization

### 1. Discarding Elements

When used alone, the slice pattern acts as a discard for a sequence of elements.

```csharp theme={"dark"}
int[] sequence = { 10, 20, 30, 40, 50 };

// Matches any sequence starting with 10 and ending with 50
bool isMatch = sequence is [10, .., 50]; 

// Matches any sequence starting with 10, 20
bool isPrefixMatch = sequence is [10, 20, ..]; 
```

### 2. Capturing Elements

When combined with a `var` declaration or a specific type, the slice pattern captures the matched elements into a new variable.

```csharp theme={"dark"}
int[] sequence = { 10, 20, 30, 40, 50 };

// Captures the elements between the first and last into 'middle'
if (sequence is [10, .. var middle, 50])
{
    // 'middle' is evaluated as int[] containing { 20, 30, 40 }
}
```

### 3. Applying Sub-patterns

The slice pattern can be followed by another pattern to evaluate the matched sub-sequence without necessarily capturing it.

```csharp theme={"dark"}
int[] sequence = { 10, 20, 30, 40, 50 };

// Applies a property pattern to ensure exactly 3 elements are in the slice
bool hasThreeMiddleElements = sequence is [10, .. { Length: 3 }, 50];

// Applies a list pattern to validate the exact contents of the slice
bool matchesExactMiddle = sequence is [10, .. [20, 30, 40], 50];
```

### 4. Zero-Element Matching

The slice pattern dynamically adjusts to the length of the sequence. If the explicit positional patterns consume the entire sequence, the slice pattern resolves to an empty collection.

```csharp theme={"dark"}
int[] shortSequence = { 10, 50 };

// Evaluates to true. 'middle' is captured as an empty int[] { }
bool isZeroMatch = shortSequence is [10, .. var middle, 50];
```

## Static Type Resolution Rules

To use a list pattern at all, a type must be countable (possessing an accessible `Count` or `Length` property) and indexable (possessing an accessible `int` indexer).

However, when capturing a slice (e.g., `.. var slice`) or applying a sub-pattern to it (e.g., `.. { Length: 3 }`), the type must additionally be "sliceable". The type of the matched sub-sequence is determined **strictly at compile time** based on the **static type** of the collection being matched.

The compiler evaluates the following criteria to extract the slice:

1. **It is an array type (`T[]`):** The compiler has intrinsic support for arrays. It emits a call to `System.Runtime.CompilerServices.RuntimeHelpers.GetSubArray`, which allocates and returns a new array (`T[]`) containing the sliced elements.
2. **It is `string`:** The compiler has intrinsic support for strings. It emits a call to `string.Substring`, which allocates and returns a new `string`.
3. **It has an accessible `Range` indexer:** The compiler invokes the indexer passing a `System.Range` parameter. The captured type matches the return type of the indexer.
4. **It has an accessible `Slice(int, int)` method:** The compiler invokes this method, passing the start index and length. The captured type matches the return type of the `Slice` method. This is utilized by types like `Span<T>` and `ReadOnlySpan<T>` to return a new span without heap allocations.

If the static type supports basic list patterns but does not meet any of these sliceable criteria (e.g., the `IList<T>` interface), the slice capture or sub-pattern application will fail to compile.

```csharp theme={"dark"}
string text = "A123Z";

// 'stringSlice' is strictly typed as string.
// The compiler emits a call to string.Substring (allocates).
if (text is ['A', .. var stringSlice, 'Z']) 
{
    // stringSlice contains "123"
}

// 'spanSlice' is strictly typed as ReadOnlySpan<char>.
// The compiler emits a call to ReadOnlySpan<T>.Slice(int, int) (no allocation).
if (text.AsSpan() is ['A', .. var spanSlice, 'Z']) 
{
    // spanSlice contains "123"
}

IList<int> list = new int[] { 1, 2, 3, 4 };

// COMPILER ERROR: IList<int> supports list patterns (has Count and this[int]),
// but it is not sliceable (lacks a Range indexer or Slice method).
// The runtime type (int[]) is not evaluated for the capture.
if (list is [1, .. var listSlice, 4]) 
{
}

// This compiles successfully because no slice capture/sub-pattern is used.
if (list is [1, 2, 3, 4])
{
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
