Skip to content

[API Proposal]: MemoryExtension.Contains overload #135684

Open

Description

@ladeak

Background and motivation

I would like to check if a byte sequence contains another byte sequence.

Today I have to:

MemoryExtensions.IndexOf(myData, [(byte)1, (byte)1, (byte)1]) > -1 

There are Contains method overloads on MemoryExtensions, but no overload for a generic ReadOnlySpan<T> (there is a ReadOnlySpan<char> overload).

API Proposal

namespace System;

public partial class MemoryExtensions
{
+    public static unsafe bool Contains<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T>?
}

I presume the implementation will require unsafe.

API Usage

var myData = new byte[100];
Assert.NotEqual(-1, MemoryExtensions.IndexOf(myData, [(byte)0, (byte)3, (byte)101, (byte)102, (byte)103]));

to become:

var myData = new byte[100];
Assert.True(MemoryExtensions.Contains(myData, [(byte)0, (byte)3, (byte)101, (byte)102, (byte)103]));

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Oct 13, 2026
  2. dotnet-policy-service commented on Oct 13, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-memory
    See info in area-owners.md if you want to be subscribed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.MemoryuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions