zoukankan      html  css  js  c++  java
  • C#之delegate

    delegate 委托的使用:

    封装一个方法,该方法只有一个参数并且不返回值。

    using System;
    using System.Windows.Forms;
    
    delegate void DisplayMessage(string message);
    
    public class TestCustomDelegate
    {
       public static void Main()
       {
          DisplayMessage messageTarget; 
    
          if (Environment.GetCommandLineArgs().Length > 1)
             messageTarget = ShowWindowsMessage;
          else
             messageTarget = Console.WriteLine;
    
          messageTarget("Hello, World!");   
       }      
    
       private static void ShowWindowsMessage(string message)
       {
          MessageBox.Show(message);      
       }
    }
    using System;
    using System.Windows.Forms;
    
    public class TestAction1
    {
       public static void Main()
       {
          Action<string> messageTarget; 
    
          if (Environment.GetCommandLineArgs().Length > 1)
             messageTarget = ShowWindowsMessage;
          else
             messageTarget = Console.WriteLine;
    
          messageTarget("Hello, World!");   
       }      
    
       private static void ShowWindowsMessage(string message)
       {
          MessageBox.Show(message);      
       }
    }
    using System;
    using System.Windows.Forms;
    
    public class TestAnonMethod
    {
       public static void Main()
       {
          Action<string> messageTarget; 
    
          if (Environment.GetCommandLineArgs().Length > 1)
             messageTarget = delegate(string s) { ShowWindowsMessage(s); };
          else
             messageTarget = delegate(string s) { Console.WriteLine(s); };
    
          messageTarget("Hello, World!");
       }
    
       private static void ShowWindowsMessage(string message)
       {
          MessageBox.Show(message);      
       }
    }
    using System;
    using System.Windows.Forms;
    
    public class TestLambdaExpression
    {
       public static void Main()
       {
          Action<string> messageTarget; 
    
          if (Environment.GetCommandLineArgs().Length > 1)
             messageTarget = s => ShowWindowsMessage(s); 
          else
             messageTarget = s => Console.WriteLine(s);
    
          messageTarget("Hello, World!");
       }
    
       private static void ShowWindowsMessage(string message)
       {
          MessageBox.Show(message);      
       }
    }
    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            List<String> names = new List<String>();
            names.Add("Bruce");
            names.Add("Alfred");
            names.Add("Tim");
            names.Add("Richard");
    
            // Display the contents of the list using the Print method.
            names.ForEach(Print);
    
            // The following demonstrates the anonymous method feature of C#
            // to display the contents of the list to the console.
            names.ForEach(delegate(String name)
            {
                Console.WriteLine(name);
            });
        }
    
        private static void Print(string s)
        {
            Console.WriteLine(s);
        }
    }
    /* This code will produce output similar to the following:
     * Bruce
     * Alfred
     * Tim
     * Richard
     * Bruce
     * Alfred
     * Tim
     * Richard
     */
  • 相关阅读:
    xml技术基础
    apue第七章学习总结
    自然连接,外部连接,内连接,左右连接的区别与介绍(转)
    apue第六章学习总结
    《深入PHP与jQuery开发》读书笔记——Chapter2
    apue第四章学习总结
    《深入PHP与jQuery开发》读书笔记——Chapter1
    我所使用的一个通用的Makefile模板
    PHP的输出缓冲区(转)
    Nginx 日志按月分割
  • 原文地址:https://www.cnblogs.com/xbblogs/p/6151084.html
Copyright © 2011-2022 走看看