C # delegation (1): What is delegation?

Source: Internet
Author: User

This series of articles will discuss in detail the delegation in C #, list its main implementation methods, and analyze its benefits at the design level and coding level, finally, we will discuss security and execution efficiency. What is delegation? The delegate is the. NET version of The addressing method. you can use the delegate to pass the method as a parameter. A delegate is a special type of object. Its special feature is that a delegate contains only the addresses of multiple active methods, rather than data. Although a delegate looks like a type, a delegate is actually defined as a new class. The following code defines a delegate and uses ildasm.exe to view its generated IL code: // defines the delegate. It defines the types of methods that can be represented, but it is a class of public delegate int methodDelegate (string str ); 032101 as shown in the red box in the figure ,. NET will be defined as a sealed class, derived from the base class System. multicastDelegate inherits the three methods of the base class (discussed later ). Differences between delegation and function pointers 1. Security: the C/C ++ function pointer extracts the function address and passes it as a parameter without type security, any function can be passed to the place where the function pointer is needed.. NET. 2. relevance with instances: in Object-Oriented Programming, almost no method exists in isolation, but usually needs to be associated with class instances before calling methods. The delegate can obtain information in the class instance to associate with the instance. 3. Essentially, a function pointer is a pointer variable that is allocated to the stack. A delegate type declares a class, instantiated as an object, and allocated to the heap. 4. A delegate can point to a function with the same parameters and signatures in different classes. function pointers are not allowed. Copy the code namespace ConsoleApplication1 {// define the delegate, which defines the types of methods that can be represented, but it is itself a class public delegate void methodDelegate (string str ); class Program {static void Main (string [] args) {Student student = new Student (); Teacher teacher = new Teacher ("instructor Wang"); methodDelegate methodDelegate1 = new methodDelegate (student. getStudentName); methodDelegate1 + = teacher. getTeacherName; // you can point to methods in different classes! // MethodDelegate1 + = teacher. getClassName; an error is prompted when it points to a method that does not match the signature! MethodDelegate1.Invoke ("James"); Console. readLine () ;}} class Student {private String name = ""; public Student (String _ name) {this. name = _ name;} public Student () {} public void getStudentName (String _ name) {if (this. name! = "") Console. writeLine ("Student's name is {0}", this. name); else Console. writeLine ("Student's name is {0}", _ name) ;}} class Teacher {private String name; public Teacher (String _ name) {this. name = _ name;} public void getTeacherName (String _ name) {if (this. name! = "") Console. writeLine ("Teacher's name is {0}", this. name); else Console. writeLine ("Teacher's name is {0}", _ name);} public string getClassName () {return "Eanlish ";}}}

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.