Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1010557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:06:05+00:00 2026-05-16T09:06:05+00:00

I had a need for a method that could take a collection of strings,

  • 0

I had a need for a method that could take a collection of strings, and replace all occurrences of a specific string with another.

For example, if I have a List<string> that looks like this:

List<string> strings = new List<string> { "a", "b", "delete", "c", "d", "delete" };

and I want to replace “delete” with “”, I would use this LINQ statement:

strings = (from s in strings select (s=="delete" ? s=String.Empty : s)).ToList();

and it works great. But then I figured I should make it an extension method, since I’d likely use it again later. In this case, I just want to write the following:

strings.ReplaceStringInListWithAnother( "delete", String.Empty);

While my code compiles, and the LINQ statement works inside of the extension method, when I return the collection reverts back to its original contents:

public static void ReplaceStringInListWithAnother( this List<string> my_list, string to_replace, string replace_with)
{
    my_list = (from s in my_list select (s==to_replace ? s=replace_with : s)).ToList();
}

So it would seem that I just modified a copy of the List… but when I looked at the code for Pop, it modifies the collection similarly, yet the changes stick, so my assumption was that my method’s parameter declarations are correct.

Can anyone explain what I am doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:06:06+00:00Added an answer on May 16, 2026 at 9:06 am

    The LINQ statement you wrote does not modify the collection, it actually creates a new one.

    The extension method you wrote creates this new collection and then discards it. The assignment is redundant: you’re assigning to a local parameter, which goes out of scope immediately after.

    When you’re calling the method, you’re also discarding its result instead of assigning it back.

    Therefore, you should write the method like this:

    public static List<string> ReplaceStringInListWithAnother(
        this List<string> my_list, string to_replace, string replace_with)
    {
        return (from s in my_list select
            (s == to_replace ? replace_with : s)).ToList();
    }
    

    and the call like this:

    strings = strings.ReplaceStringInListWithAnother("delete", "");
    

    By the way, you can make the function more useful by making it generic:

    public static List<T> ReplaceInList<T>(this List<T> my_list,
        T to_replace, T replace_with) where T : IEquatable<T>
    {
        return (from s in my_list select
            (s.Equals(to_replace) ? replace_with : s)).ToList();
    }
    

    This way you can use it for other things, not just strings. Furthermore, you can also declare it to use IEnumerable<T> instead of List<T>:

    public static IEnumerable<T> ReplaceItems<T>(this IEnumerable<T> my_list,
        T to_replace, T replace_with) where T : IEquatable<T>
    {
        return from s in my_list select (s.Equals(to_replace) ? replace_with : s);
    }
    

    This way you can use it for any collection of equatable items, not just List<T>. Notice that List<T> implements IEnumerable<T>, so you can still pass a List into this function. If you want a list out, simply call .ToList() after the call to this one.

    Update: If you actually want to replace elements in a list instead of creating a new one, you can still do that with an extension method, and it can still be generic, but you can’t use Linq and you can’t use IEnumerable<T>:

    public static void ReplaceInList<T>(this List<T> my_list,
        T to_replace, T replace_with) where T : IEquatable<T>
    {
        for (int i = 0; i < my_list.Count; i++)
            if (my_list[i].Equals(to_replace))
                my_list[i] = replace_with;
    }
    

    This will not return the new list, but instead modify the old one, so it has a void return type like your original.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've had an ongoing need here that I can't figure out how to address
I had a need for a certain functionality on my web apps and I
I've recently had a need to do a bit of lisp editing and I
I've never had much need for programming with databases. Since their use is so
I recently had a need to interpret a DEC 32-bit floating point representation. It
I've never really had the need to use any drag functions until now, so
I have not had to mess with mailto links much. However I now need
Had an interesting discussion with some colleagues about the best scheduling strategies for realtime
Had a coworker ask me this, and in my brain befuddled state I didn't
I had used Server Explorer and related tools for graphical database development with Microsoft

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.