Skip to content

[API Proposal]: Change Process.Run() and related methods to accept IEnumerable<string> instead of IList<string> #130364

Closed

Description

@TheTomatoKing

Background and motivation

The new Process.Run() and related methods take an IList<string> for command arguments.
It should use IEnumerable<string> instead for correctness, and to be in line with the Process.Start() counterpart.

API Proposal

namespace System.Diagnostics
{
    public partial class Process
    {
        public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = default);
-       public static ProcessExitStatus Run(string fileName, IList<string>? arguments = null, bool silent = false, TimeSpan? timeout = default);
+       public static ProcessExitStatus Run(string fileName, IEnumerable<string>? arguments = null, bool silent = false, TimeSpan? timeout = default);
        public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, TimeSpan? timeout = default);
-       public static ProcessTextOutput RunAndCaptureText(string fileName, IList<string>? arguments = null, TimeSpan? timeout = default);
+       public static ProcessTextOutput RunAndCaptureText(string fileName, IEnumerable<string>? arguments = null, TimeSpan? timeout = default);
        public static Task<ProcessTextOutput> RunAndCaptureTextAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
-       public static Task<ProcessTextOutput> RunAndCaptureTextAsync(string fileName, IList<string>? arguments = null, CancellationToken cancellationToken = default);
+       public static Task<ProcessTextOutput> RunAndCaptureTextAsync(string fileName, IEnumerable<string>? arguments = null, CancellationToken cancellationToken = default);
        public static Task<ProcessExitStatus> RunAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
-       public static Task<ProcessExitStatus> RunAsync(string fileName, IList<string>? arguments = null, bool silent = false, CancellationToken cancellationToken = default);
+       public static Task<ProcessExitStatus> RunAsync(string fileName, IEnumerable<string>? arguments = null, bool silent = false, CancellationToken cancellationToken = default);
        public bool Start();
        public static Process? Start(ProcessStartInfo startInfo);
        public static Process Start(string fileName);
        public static Process Start(string fileName, string? arguments);
        public static Process Start(string fileName, IEnumerable<string> arguments);
        public static int StartAndForget(ProcessStartInfo startInfo);
-       public static int StartAndForget(string fileName, IList<string>? arguments = null);
+       public static int StartAndForget(string fileName, IEnumerable<string>? arguments = null);
    }
}

API Usage

No change for existing callers.

Alternative Designs

IReadOnlyList<string> is also a valid option.
But IEnumerable<string> will allow it to be dropped in as a replacement for more existing code, and I can see neither Count nor the indexer are used in the current implementation.

Risks

This is a breaking change. But I don't think it's beyond what's expected in Preview/RC.

Activity

  1. added
    api-suggestion
    on Jul 8, 2026
  2. added
    untriaged
    on Jul 8, 2026
  3. dotnet-policy-service commented on Jul 8, 2026

    @dotnet-policy-service
    Contributor

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

  4. teo-tsirpanis commented on Jul 8, 2026

    @teo-tsirpanis
    Contributor

    Accepting IEnumerable<string> would be quite restrictive and preclude the implementation code from enumerating it multiple times, or getting its length. IReadOnlyList<string> would be better, but not much better than the status quo.

  5. dotnet-policy-service commented on Jul 8, 2026

    @dotnet-policy-service
    Contributor

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

  6. TheTomatoKing commented on Jul 8, 2026

    @TheTomatoKing
    Author

    @teo-tsirpanis
    I think it's worth encouraging refactors from older Process.Start() patterns to the new Process.Run() methods where possible.
    Especially when reading output of a process. Since that can cause deadlocks if not careful, and probably does in a lot of existing code.
    This is why I suggested making Process.Run() take an IEnumerable<string>, since that would be a very easy change and make it a drop-in replacement in even more cases.

    I do see the point that future implementation could take advantage of IReadOnlyList<string>'s interface. So that may be the better choice in the long run.

    But to me the use of IList<string> indicates an intent for the method to possibly modify the collection. Which in this case it should never do.
    So either of IReadOnlyList<string> or IEnumerable<string> would be more correct. And IMO any public library should try to promote such "correctness" where possible, especially core libraries.

    With the way it currently works, if I write a method that wraps Process.Run(), I have the following options:

    • Take an IList<string> parameter instead of IReadOnlyList<string>. Potentially making it ambiguous whether the collection will be modified.
    • Take an IReadOnlyList<string> and copy it to an intermediary list, which Process.Run() will then create its own copy of.
    • Use older more complex APIs, which Process.Run() was designed to simplify.

    Considering how common that is, isn't it worth getting it right, so people can write simpler and more correct code?
    Wasn't that the point of these methods in the first place? And isn't the point of Preview versions to get it right before GA?

  7. huoyaoyuan commented on Jul 9, 2026

    @huoyaoyuan
    Member

    The pre-existing ProcessStartInfo constructor and Process.Start overload are both using IEnumerable<string>.

  8. JakeYallop commented on Jul 9, 2026

    @JakeYallop
    Contributor

    Is it critical that the list isn't modified after being passed in? If that is the case, then IEnumerable + taking a defensive copy feels better to me as well (or using some other read only interface).

  9. TheTomatoKing commented on Jul 9, 2026

    @TheTomatoKing
    Author

    @JakeYallop The current implementation creates a defensive copy using a forecah loop. Besides that, it doesn't touch the original collection.

  10. JakeYallop commented on Jul 9, 2026

    @JakeYallop
    Contributor

    @JakeYallop The current implementation creates a defensive copy using a forecah loop. Besides that, it doesn't touch the original collection.

    Thanks.

    That makes the choice of IList<string> feel even weirder to me then.

  11. TheTomatoKing commented on Jul 9, 2026

    @TheTomatoKing
    Author

    @JakeYallop It gets even stranger.
    Similar to it's Process.Start() equivalent, it uses ProcessStartInfo under the hood.
    But unlike Process.Start(), it neglects to use the ProcessStartInfo(string fileName, IEnumerable<string> arguments) ctor which uses .ToList() to create the defensive copy and populate ProcessStartInfo.ArgumentList in one go.
    Instead, Process.Run() forecahes over the collection, populating ProcessStartInfo.ArgumentList one item at a time.
    That isn't a huge deal. But the whole thing put together smells of AI code with lack of proper review to me.

  12. adamsitnik commented on Jul 13, 2026

    @adamsitnik
    Member

    Hi @TheTomatoKing

    It should use IEnumerable<string> instead for correctnes

    But the whole thing put together smells of AI code with lack of proper review to me.

    Thank you for your feedback. The new APIs are correct (arguments provided by the users are not modified), but as you have pointed out, not consistent with the older APIs (that accept IEnumerable<string>). The reasons for that is not "AI code with lack of proper review" (LOL). I just wanted them to avoid hidden allocations like this:

    _argumentList = new Collection<string>(new List<string>(arguments));

    Having said that, I am going to present this API update to the API Review Board. We don't have much time (code complete for the last preview is 16th of July) so I am going to mark this proposal as blocking.

    Thanks

  13. 9 remaining items

  14. added this to the 11.0.0 milestone on Jul 14, 2026
  15. ladeak commented on Jul 14, 2026

    @ladeak
    Contributor

    Could this have params, so that the parameter would be: params IEnumerable<string>? arguments = null ? It would cause an issue with the order or parameters, but when I tried to use the API in the current preview it would have felt natural to pass string arguments as params as opposed to a collection expression.

  16. adamsitnik commented on Jul 21, 2026

    @adamsitnik
    Member

    Could this have params, so that the parameter would be: params IEnumerable<string>? arguments = null ? It would cause an issue with the order or parameters, but when I tried to use the API in the current preview it would have felt natural to pass string arguments as params as opposed to a collection expression.

    We provide sync and async overloads, IIRC the CancellationToken needs to be the last argument for async methods, so we could not use params IEnumerable<string> for the async overloads.

    We could do it for StartAndForget (no other arguments or async overload), but I am not sure whether it's worth it. And also what is the story for param IEnumerable in F#

  17. bartonjs commented on Jul 21, 2026

    @bartonjs
    Member

    This increases consistency of the Process class, looks good as proposed.

    namespace System.Diagnostics
    {
        public partial class Process
        {
            public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = default);
    -       public static ProcessExitStatus Run(string fileName, IList<string>? arguments = null, bool silent = false, TimeSpan? timeout = default);
    +       public static ProcessExitStatus Run(string fileName, IEnumerable<string>? arguments = null, bool silent = false, TimeSpan? timeout = default);
            public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, TimeSpan? timeout = default);
    -       public static ProcessTextOutput RunAndCaptureText(string fileName, IList<string>? arguments = null, TimeSpan? timeout = default);
    +       public static ProcessTextOutput RunAndCaptureText(string fileName, IEnumerable<string>? arguments = null, TimeSpan? timeout = default);
            public static Task<ProcessTextOutput> RunAndCaptureTextAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
    -       public static Task<ProcessTextOutput> RunAndCaptureTextAsync(string fileName, IList<string>? arguments = null, CancellationToken cancellationToken = default);
    +       public static Task<ProcessTextOutput> RunAndCaptureTextAsync(string fileName, IEnumerable<string>? arguments = null, CancellationToken cancellationToken = default);
            public static Task<ProcessExitStatus> RunAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
    -       public static Task<ProcessExitStatus> RunAsync(string fileName, IList<string>? arguments = null, bool silent = false, CancellationToken cancellationToken = default);
    +       public static Task<ProcessExitStatus> RunAsync(string fileName, IEnumerable<string>? arguments = null, bool silent = false, CancellationToken cancellationToken = default);
            public static int StartAndForget(ProcessStartInfo startInfo);
    -       public static int StartAndForget(string fileName, IList<string>? arguments = null);
    +       public static int StartAndForget(string fileName, IEnumerable<string>? arguments = null);
        }
    }
  18. added
    api-approved
    and removed
    blocking
    api-ready-for-review
    on Jul 21, 2026
  19. self-assigned this
    on Jul 21, 2026
  20. added a commit that references this issue on Jul 21, 2026
    640b31b
  21. adamsitnik commented on Jul 22, 2026

    @adamsitnik
    Member

    The change will be included in .NET 11 Preview 7.

  22. omariom commented on Jul 22, 2026

    @omariom
    Contributor

    IReadOnlyList materializes before the API call, while IEnumerable materializes inside it.
    With a failing LINQ query, this can make the API appear in the call stack even when it isn't the cause.

  23. huoyaoyuan commented on Jul 22, 2026

    @huoyaoyuan
    Member

    With a failing LINQ query, this can make the API appear in the call stack even when it isn't the cause.

    This is applicable to any API accepting IEnumerable.

  24. locked and limited conversation to collaborators on Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Diagnostics.Process

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions