zoukankan      html  css  js  c++  java
  • delegate (C# Reference)

    The declaration of a delegate type takes the following form:

    public delegate void TestDelegate(string message);

    The delegate keyword is used to declare a reference type that can be used to encapsulate a named or an anonymous method. Delegates are similar to function pointers in C++; however, delegates are type-safe and secure. For applications of delegates, see Delegates and Generic Delegates.

    Remarks

    Delegates are the basis for Events.

    A delegate can be instantiated by associating it either with a named or anonymous method. For more information, see Named Methods and Anonymous Methods.

    For use with named methods, the delegate must be instantiated with a method that has an acceptable signature. For more information on the degree of variance that is allowed in the method signature, see Covariance and Contravariance in Delegates. For use with anonymous methods, the delegate and the code to be associated with it are declared together. Both ways of instantiating delegates are discussed in this section.


    代码
    using System;
    // Declare delegate -- defines required signature:
    delegate void SampleDelegate(string message);

    class MainClass
    {
        
    // Regular method that matches signature:
        static void SampleDelegateMethod(string message)
        {
            Console.WriteLine(message);
        }

        
    static void Main()
        {
            
    // Instantiate delegate with named method:
            SampleDelegate d1 = SampleDelegateMethod;
            
    // Instantiate delegate with anonymous method:
            SampleDelegate d2 = delegate(string message)
            { 
                Console.WriteLine(message); 
            };

            
    // Invoke delegate d1:
            d1("Hello");
            
    // Invoke delegate d2:
            d2(" World");
        }
    }


  • 相关阅读:
    安装python包
    在RHEL5.4上升级Python
    IronPython开发Windows Form程序总结
    Windows下手动配置Oracle Client的要点
    dreampie一个很不错的python命令行交互工具
    Eclipse插件汇总
    pyDbRowFactory Python版Db Row Factory
    如何让Jython自动加载一个Jar包
    跨计算机执行的几个方法
    Python 版 Instance Activator
  • 原文地址:https://www.cnblogs.com/sandy_liao/p/1891503.html
Copyright © 2011-2022 走看看