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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:59:27+00:00 2026-05-11T11:59:27+00:00

When trying to use delegates in C# to solve a problem in a functional

  • 0

When trying to use delegates in C# to solve a problem in a functional way, I’ve come across a pitfall that I want to share resp. for which I would like to hear your suggestions.

Background

I want to fill a grid from a list of objects where the values for single columns are get using delegates (idea borrowed from Philip Pipers ObjectListView control).

Additionally I want to automatically insert columns containing the (numerical) difference between two values.

So my objects having properties FirstValue, SecondValue and ThirdValue I want to have columns with FirstValue, (SecondValue-FirstValue), SecondValue, (ThirdValue-SecondValue), ThirdValue.

I already have adapted an existing grid control to use delegates on an object list, this part works fine.

First attempt

First, I tried something like:

class MyGridClass : DelegateGrid {   DelegateGrid.ValueGetter lastGetter;    public MyGridClass() {     AddMyColumn(delegate(MyObj obj) { return obj.FirstValue; });     AddMyColumn(delegate(MyObj obj) { return obj.SecondValue; });     AddMyColumn(delegate(MyObj obj) { return obj.ThirdValue; });   }    private void AddMyColumn(DelegateGrid.ValueGetter getter) {     if (lastGetter != null)       base.AddColumn(new DelegateColumn(delegate(MyObj obj) {          return getter(obj)-lastGetter(obj);        }));     base.AddColumn(new DelegateColumn(getter));   } }; 

Problem

In a functional language, calculating the difference in this way would work fine, since the new delegate (constructed inside AddMyColumn) would use the value of lastGetter at the time of construction. But in C#, the new delegate uses a reference to lastGetter, so when executed, it uses the actual value at the time of execution. So the difference will always be built against the last column (i.e. obj.ThirdValue).

Solution

One solution I’ve found for myself is

public AddMyColumn(DelegateGrid.ValueGetter getter) {   if (lastGetter != null) {     DelegateGrid.ValueGetter newLastGetter =        new DelegateGrid.ValueGetter(lastGetter);     base.AddColumn(new DelegateColumn(delegate(MyObj obj) {       return getter(obj)-newLastGetter(obj);      }));   }   // ... } 

Note that

if (lastGetter != null) {   DelegateGrid.ValueGetter newLastGetter =      delegate(MyObject obj){return lastGetter(obj); }; 

wouldn’t have solved the problem.

Question

Already having found a solution, this part is a bit pro forma, but

  • Does anyone have a suggestion for a better solution
  • I’m using C#2.0 and have only a theoretical knowledge of lambda expressions in C#3.0: Would they allow for a cleaner solution (and thus deserve their name…)?
  • 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. 2026-05-11T11:59:28+00:00Added an answer on May 11, 2026 at 11:59 am

    The problem is just that the variable is being captured rather than the value. Here’s a solution which is much the same, but slightly simpler:

    public AddMyColumn(DelegateGrid.ValueGetter getter) {   if (lastGetter != null) {     DelegateGrid.ValueGetter newLastGetter = lastGetter;     base.AddColumn(new DelegateColumn(delegate(MyObj obj) {       return getter(obj)-newLastGetter(obj);      }));   }   // ... } 

    Basically there’s no need to create a new delegate instance – delegates are immutable, so you can just copy the value with assignment.

    This isn’t really a delegate-specific problem in terms of the value being captured – it’s a common problem for anonymous methods and lambda expressions in general. The typical example is;

    List<Action> actions = new List<Action>(); for (int i=0; i < 10; i++) {     actions.Add(() => Console.WriteLine(i)); } foreach (Action action in actions) {     action(); } 

    This prints ’10’ 10 times. To print 0-9, you again need to change the scope of the captured variable:

    List<Action> actions = new List<Action>(); for (int i=0; i < 10; i++) {     int copy = i;     actions.Add(() => Console.WriteLine(copy)); } foreach (Action action in actions) {     action(); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 92k
  • Answers 92k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This link looks promising: Getting google page rank using javascript May 11, 2026 at 6:31 pm
  • Editorial Team
    Editorial Team added an answer Most games don't wait for events. They poll the input… May 11, 2026 at 6:31 pm
  • Editorial Team
    Editorial Team added an answer The reason you separate the login and non-login shell is… May 11, 2026 at 6:31 pm

Related Questions

I would like to implement something similar to a c# delegate method in PHP.
I'm trying to do something I've been historically available to do using Visual FoxPro
I'm trying to write a super-simple podcast-to-device downloading service to use for running. I
I'm trying to use an unmanaged dll in VB.NET. The example source code provided

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.