© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•17mo ago•
11 replies
The Father

✅ confusing list logic

So, I have a program that allows me delete objects in an array through the console, and the objects are all displayed to the console like in this image (AT THE BOTTOM)

And for the objects that I want to delete, I need to the 1-based indices of the objects I want to delete, or a range of indices that i want to delete

But I'm having a problem with the code: each iteration deletes objects before the next iteration/deletion, interrupting the next iteration/deletion

public static void PromptDeletion<T>(ref T[] values, Func<T, string> repr)
{
    List<T> tempValues = new(values);

    ShowValuesInOrder([.. tempValues], repr);

    Console.WriteLine("Values to delete> ");
    string input = Console.ReadLine() ?? "";

    string[] deletions = input.Split(',');
    // Tries to delete the objects at the specified indices, but each iteration removes objects from the array BEFORE the NEXT iteration/deletion
    try
    {
        foreach (string value in deletions)
        {
            if (value.Count(s => s == '-') == 1)
            {
                int[] ends = value.Split('-').Select(n => Convert.ToInt32(n) - 1).ToArray();
                tempValues.RemoveRange(ends[0], ends[1] - ends[0]);
            }
            else
            {
                tempValues.RemoveAt(Convert.ToInt32(value));
            }
        }

        values = [.. tempValues];

    }
    catch (Exception)
    {
        ConsoleExt.WriteError("There was a problem removing the values.");
    }
}
public static void PromptDeletion<T>(ref T[] values, Func<T, string> repr)
{
    List<T> tempValues = new(values);

    ShowValuesInOrder([.. tempValues], repr);

    Console.WriteLine("Values to delete> ");
    string input = Console.ReadLine() ?? "";

    string[] deletions = input.Split(',');
    // Tries to delete the objects at the specified indices, but each iteration removes objects from the array BEFORE the NEXT iteration/deletion
    try
    {
        foreach (string value in deletions)
        {
            if (value.Count(s => s == '-') == 1)
            {
                int[] ends = value.Split('-').Select(n => Convert.ToInt32(n) - 1).ToArray();
                tempValues.RemoveRange(ends[0], ends[1] - ends[0]);
            }
            else
            {
                tempValues.RemoveAt(Convert.ToInt32(value));
            }
        }

        values = [.. tempValues];

    }
    catch (Exception)
    {
        ConsoleExt.WriteError("There was a problem removing the values.");
    }
}

I'm looking for a way to remove all of the specified objects from the array all at once, or something else that achieves the same result
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Timers are confusing
C#CC# / help
3y ago
Logic error
C#CC# / help
2y ago