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
     */
  • 相关阅读:
    在autolayout中加入每个view的weight
    iOS 拨打电话
    20141211笔记(UIImageView 设置内容的Mode的方法UICollectionViewCell Custom的方法ios modal segue code)
    UILabel总结(转载)
    Error:duplicate files during packaging of APK app/build/output/apk
    《UNIX-Shell编程24学时教程》读书笔记Chap3,4 文件,目录操作
    《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础
    《UNIX-Shell编程24学时教程》读书笔记chap7 变量
    《软件调试的艺术》读书笔记
    ubuntu环境准备
  • 原文地址:https://www.cnblogs.com/xbblogs/p/6151084.html
Copyright © 2011-2022 走看看