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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:51:17+00:00 2026-06-15T00:51:17+00:00

In C# language and .NET framework, could you help me with understanding delegates? I

  • 0

In C# language and .NET framework, could you help me with understanding delegates?
I was trying to check some code, and found that the results I received were unexpected for me. Here it is:

class Program
{
    public static int I = 0;

    static Func<string> del = new Func<string>(I.ToString);

    static void Main(string[] args)
    {
        I = 10;
        Console.WriteLine("{0}", del());
    }
}

The answer was 0, but not 10. Why?

  • 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-15T00:51:18+00:00Added an answer on June 15, 2026 at 12:51 am

    The reason is the following:

    The way you declare the delegate it points directly to the ToString method of the static int instance. It is captured at the time of creation.

    As flindeberg points out in the comments below, each delegate has a target and a method to be executed on the target.

    In this case, the method to be executed is obviously the ToString method. The interesting part is the instance the method is executed on: It is the instance of I at the time of the creation, meaning that the delegate is not using I to get the instance to use but it stores the reference to the instance itself.

    Later you change I to a different value, basically assigning it a new instance. This doesn’t magically change the instance captured in your delegate, why should it?

    To get the result you expect, you would need to change the delegate to this:

    static Func<string> del = new Func<string>(() => I.ToString());
    

    Like this, the delegate points to an anonymous method that executes ToString on the current I at the time of the execution of the delegate.

    In this case, the method to be executed is an anonymous method created in the class in which the delegate is declared in. The instance is null as it is a static method.

    Have a look at the code the compiler generates for the second version of the delegate:

    private static Func<string> del = new Func<string>(UserQuery.<.cctor>b__0);
    private static string cctor>b__0()
    {
        return UserQuery.I.ToString();
    }
    

    As you can see, it is a normal method that does something. In our case it returns the result of calling ToString on the current instance of I.

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

Sidebar

Related Questions

I've been working on ASP.NET C# with Entity Framework and some people help me
Context Language: C# Platform Version: Microsoft .Net Framework 4.0 Operating System: Windows 7 Professional
When a new version of a framework or language appears (e.g. .NET 3.5, SQL2008),
Language = C# .NET version = 3.5 When my code performs syntax highlighting the
C:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets(2341,9): error MSB3171: Problem generating manifest. Could not load file or assembly '...CppCli.dll.manifest' or
I saw some very neat code snippets of programs implemented with .net technologies, such
I'd like to learn more how to create a language for .NET framework. I
Could you please suggest work around for .NET 4.0 defect which results into Common
Language: vb.net File size: 1GB, and stuff. Encoding of the text file: UTF8 (so
I am using vb.net language I have a dropdownlist, which is filled by below

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.