Using delegates in C#

前端 未结 5 593
深忆病人
深忆病人 2021-01-30 04:50

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.

5条回答
  •  甜味超标
    2021-01-30 05:30

    Here is how this should be done:

    using System;
    
    namespace ConsoleApplication1
    {
    
        class Program
        {
            public static int I = 0;
    
            static Func del = new Func(() => {
                return I.ToString();
            });
    
            static void Main(string[] args)
            {
                I = 10;
                Console.WriteLine("{0}", del());
            }
        }
    }
    

提交回复
热议问题