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 8417167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:57:59+00:00 2026-06-10T01:57:59+00:00

I have a variable called result which is a List<List<string>> I want to parse

  • 0

I have a variable called result which is a

List<List<string>>

I want to parse each element and fix it (remove white spaces, etc)

            i = 0;
            foreach (List<string> tr in res)
            {
                foreach (string td in tr)
                {
                    Console.Write("[{0}] ", td);
                    td = cleanStrings(td); // line with error
                    i++;
                }
                Console.WriteLine();
            }

    public string cleanStrings(string clean)
    {
        int j = 0;                
        string temp = System.Text.RegularExpressions.Regex.Replace(clean, @"[\r\n]", "");
        if (temp.Equals("&nbsp;"))
        {
            temp = " ";
            temp = temp.Trim();
        }
        clean = temp;                        
        return clean;
    }

Error 1 Cannot assign to ‘td’ because it is a ‘foreach iteration variable’

How would I fix this?

  • 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-06-10T01:58:00+00:00Added an answer on June 10, 2026 at 1:58 am

    Basically you have to not use foreach. Iterators in .NET are read-only, basically. For example:

    for (int i = 0; i < tr.Count; i++)
    {
        string td = tr[i];
        Console.Write("[{0}] ", td);
        tr[i] = CleanStrings(td);
    }
    

    (Note that I’ve used the variable i which you were incrementing but not otherwise using.)

    Alternatively, consider using LINQ:

    res = res.Select(list => list.Select(x => CleanStrings(x)).ToList())
             .ToList();
    

    Note that this creates a new list of new lists, rather than mutating any of the existing ones.

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

Sidebar

Related Questions

Okay so i have a variable called search which holds the string Find this
I have a variable called $result which should store all the results from my
I have a variable called var in SAS/IML. I want to know how I
I have a PHP-variable called $go_Adress which contains the adress I need to get
I have a class with an integer variable called layer, there is a list
I have an arraylist called 'pri1', I want this array list to be available
The scenario would be: you have a variable called person which contains a number
I have a page called index.html which takes in a few variable from the
I have a php-file called kal_test.php which gives a value to the variable $vbl
By accident I created a variable called ls which I have spotted using ls().

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.