Static data members and instance data member usages of the C # anonymous method

Source: Internet
Author: User
Tags anonymous instance method modifier

Anonymous methods always start with a delegate keyword followed by the parameters used in the method and method body itself. As you can see from the example above, the user does not need to determine the return type of the anonymous method. It is inferred from the return statement in the body of the method. The. NET CLR cannot execute free flow (flowing) code blocks like anonymous methods. CLR requirements: Each method that it executes is part of a type and should be a static (static) method or an instance (instance) method: If a method declaration contains a static modifier, the method is called a static method. If there is no static modifier, the method is called an instance method. A static method does not operate on a particular instance, and referencing this in a static method is a compile-time error. An instance method operates on a given instance of a class, and can use this to access the instance. So when you write an anonymous method in the code of a class and compile the code, the C # compiler silently creates a static or instance method in the same class that you define the anonymous method. So the anonymous method is simply a convenient syntax for defining your own methods in a class to pass to a delegate (a delegate processor/event handler).

When you compile the example above, the C # compiler creates two private static methods inside the class ' program ' where we define anonymous methods. It replaces the anonymous method with the address of these static methods at this time. The compiler determines how to create a static method or an instance method depending on the use of static or instance data members in the class that the anonymous method is defined in. In our example, we didn't use any of the class ' program ' data members because calling a static method instead of an instance method would be efficient, so the C # compiler creates a static method that encapsulates the code for our anonymous method. The following is the ILDASM view of the sample assembly ' program ' class. The highlighted section shows a new static method added silently to the ' program ' class by the C # compiler.

If we have used any static data for the ' program ' class with anonymous methods, the C # compiler will still create a static method in the ' Program ' class to wrap the anonymous method.

Instance data member usage for anonymous methods

Let's define a new instance method in the ' program ' class in our example and use the example class (The ' program ' Class) as an instance data member. The following code shows the modified example:

public class Program
{
  public delegate void MyDelegate();
  public static void Main(string[] args)
  {
   //实例数据成员测试
   Program p = new Program();
   for(int i=1;i<=5;i++)
    p.TestInstanceDataMembers();
  }
  public void TestInstanceDataMembers()
  {
   MyDelegate d = delegate
   {
    Console.WriteLine("Count: {0}",++m_iCount);
   };
   d();
  }
  public int m_iCount = 0;
}

We define a new instance method: Testinstancedatamembers, which defines an anonymous method in the ' Program ' class, where the anonymous method uses the instance data member: The M_icount of the ' Program ' class. When this example is compiled, the C # compiler creates a private instance method to wrap the anonymous method defined in Testinstancedatamembers. The C # compiler must create an instance method because the method requires access to instance data members of the ' Program ' class. The following is the ILDASM view of the sample assembly ' program ' class. The lower section of the diagram shows a new private instance method silently added by the C # compiler to the ' Program ' class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.