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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:43:55+00:00 2026-05-27T13:43:55+00:00

I am new to C# and trying to understand lambda expressions along with delegates.

  • 0

I am new to C# and trying to understand lambda expressions along with delegates. This is the code I am running:

delegate bool D1();
delegate bool D2(int i);

namespace Console
{
    class Program
    {
        D1 d1;
        D2 d2;

        public void testMethod(int input)
        {
            int j = 0;
            d1 = () => { j = 10; return j < input; };
            d2 = (x) => { return x == j; };
            System.Console.WriteLine("j = {0}", j);

            bool res = d1();
            System.Console.WriteLine("res={0}, j ={1}", res, j);

        }

        static void Main(string[] args)
        {
            Program p = new Program();
            p.testMethod(10);
            System.Console.WriteLine(p.d2(10));
            System.Console.ReadKey();
        }
    }
}

What I don’t understand is the invocation of d2 prints true. When d2 was constructed value of j was 0. It was changed only after d1 was invoked later in the testMethod. So how it is printing True ? what am I missing 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-27T13:43:55+00:00Added an answer on May 27, 2026 at 1:43 pm

    d1 and d2 both refer to the same instance of j. When you set j by calling d1 you’re also altering the value of the variable that d2 can see.

    If you want them do have different instance of j you’ll need to scope the variable:

    {
      int j = 0;
      d1 = () => { j = 10; return j < input; };
    }
    
    {
      int j = 0;
      d2 = (x) => { return x == j; };
    }
    

    However, if you’re going to do this then you might as well have two different variables, say j1 and j2 instead. This will be more readable.

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

Sidebar

Related Questions

just trying to understand this code. $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
I am new to pylon and trying to understand this piece of code: map.connect(':controller/:action/:id')
I am new in EF and trying to understand the differences in EF Code
I was trying to understand this call: deprecate :new_record?, :new? which uses this deprecate
I very new to C++ programming and trying to understand this polymorphic assignment thing.
Just trying to understand a line of code which copies a file: new File(c:\\test)
I am trying to understand J2ME code Thread aaa = new Thread(pb) { private
I am new to Python and I am trying to understand this syntax: a,
I'm new to git and I'm trying to understand if it can solve my
I just joined a new C++ software project and I'm trying to understand the

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.