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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:38:20+00:00 2026-06-04T21:38:20+00:00

My code is like this: Dictionary<string, string> specialCharacters = new Dictionary<string, string>(); specialCharacters.Add(@, %);

  • 0

My code is like this:

Dictionary<string, string> specialCharacters = new Dictionary<string, string>();
            specialCharacters.Add("@", "%");
            specialCharacters.Add("*", "^");

List<Action<Employee>> listOfDel = new List<Action<Employee>>();

foreach (KeyValuePair<string, string> character in specialCharacters)

{

Action<Employee> replace = (empData) => empData.EmpName =          empData.EmpName.ToString().Replace(character.Key, character.Value);

listOfDel.Add(replace);

//listOfDel.Add(new Action<Employee>((empData) => empData.EmpName = empData.EmpName.ToString().Replace(character.Key, character.Value)));

}     

The issue is the list listOfDel has the same action as it refers to same function replace which takes value of last pair of character.Key, character.Value ((““, “^”)
I want a result having different actions in the list of actions listOfDel , where each method will have different value present. (“@”, “%”), (“
“, “^”);
I also tried creating a new instance of action delegate and using it as anonymous method.Please see commented code, yet the problem is not solved.

  • 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-04T21:38:21+00:00Added an answer on June 4, 2026 at 9:38 pm

    The problem is that you’re capturing the iterator variable. There’s only one variable declared by the foreach loop, so by the time you execute the delegates, they’ll all be using the same value (they all refer to the same delegate). For C# 4 and earlier, you just need to create a copy:

    foreach (KeyValuePair<string, string> character in specialCharacters)  
    {
        var copy = character;
        Action<Employee> replace = empData => empData.EmpName =    
             empData.EmpName.ToString().Replace(copy.Key, copy.Value);
    
        listOfDel.Add(replace);    
    }
    

    C# 5 will render this unnecessary, as foreach will be fixed so that each iteration will have a separate variable as far as anonymous functions are concerned.

    See Eric Lippert’s blog post “Closing over the loop variable considered harmful” for more information.

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

Sidebar

Related Questions

Basically, I want something like this: Dictionary<object, string> dict = new Dictionary<object, string>(); dict.Add(null,
Given this powershell code: $drivers = New-Object 'System.Collections.Generic.Dictionary[String,String]' $drivers.Add(nitrous,vx) $drivers.Add(directx,vd) $drivers.Add(openGL,vo) Is it possible
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
I use Boost.Serialization to serialize a std::map. The code looks like this void Dictionary::serialize(std::string
I have some trivial code that looks like this: SortedDictionary<String, long> d = new
I have jquery calendar code like this <script> $(document).ready(function() { $('#calTwo').jCal({ day: new Date(
I've got some code that looks like this: valid = set() for string in
Code like this can work fine: char str[] = {'a', 'b', '\0'}; The left
The code like this: $(.hide).click(function(){ $(.online-contact).animate({width: 'toggle',height: '100%'}, fast); $(this).hide(); $(.show-class).show(); }) I want
Suppose code like this: class Base: def start(self): pass def stop(self) pass class A(Base):

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.