1.61. C# anonymous method

发布时间 :2023-10-12 23:00:07 UTC      

We have already mentioned that delegates are used to reference methods that have the same label as them. In other words, you can use a delegate object to call a method that can be referenced by a delegate.

Anonymous methods provide a technique for passing code blocks as delegate parameters. An anonymous method is a method that has no name but only a principal.

You do not need to specify a return type in an anonymous method, it is derived from the return statement inferred.

1.61.1. Write the syntax for anonymous methods #

Anonymous methods are done by using the delegate keyword to create a delegate instance to declare. For example:

delegate void NumberChanger(int n);
...
NumberChanger nc = delegate(int x)
{
    Console.WriteLine("Anonymous Method: {0}", x);
};

Code block Console.WriteLine("Anonymous Method: {0}", x); is the bodyof an anonymous method.

A delegate can be called either through an anonymous method or through a named method call, that is, by passing method parameters to the delegate object.

Note: the body of an anonymous method needs a ; .

For example:

nc(10);

1.61.2. Example #

The following example demonstrates the concept of anonymous methods:

Example #

using System;

delegate void NumberChanger(int n);
namespace DelegateAppl
{
    class TestDelegate
    {
        static int num = 10;
        public static void AddNum(int p)
        {
            num += p;
            Console.WriteLine("Named Method: {0}", num);
        }

        public static void MultNum(int q)
        {
            num *= q;
            Console.WriteLine("Named Method: {0}", num);
        }

        static void Main(string[] args)
        {
            // Creating a delegate instance using anonymous methods
            NumberChanger nc = delegate(int x)
            {
               Console.WriteLine("Anonymous Method: {0}", x);
            };

            // Using anonymous methods to call delegates
            nc(10);

            // Instantiating delegates using naming methods
            nc =  new NumberChanger(AddNum);

            // Calling delegates using named methods
            nc(5);

            // Instantiating a delegate using another naming method
            nc =  new NumberChanger(MultNum);

            // Calling delegates using named methods
            nc(2);
            Console.ReadKey();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Anonymous Method: 10
Named Method: 15
Named Method: 30

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.