.net3.5,4.0中定义了Action关键字
封装一个方法,该方法不具有参数并且不返回值。
使用此委托以参数形式传递方法,不必显式定义一个封装无参数过程的委托。
若要引用无参数并返回值 的方法,请改用泛型 Func<TResult>委托。
using System;
using System.Windows.Forms;
public delegate void ShowValue();
public class Name {
private string instanceName; public Name(string name) { this.instanceName = name; }
public void DisplayToWindow() { MessageBox.Show(this.instanceName);
}
}
public class testTestDelegate {
public static void Main() { Name testName = new Name("Koani"); ShowValue showMethod = testName.DisplayToWindow;
showMethod();
}
}
public delegate void ShowValue();
public class Name {
private string instanceName; public Name(string name) { this.instanceName = name; }
public void DisplayToWindow() { MessageBox.Show(this.instanceName);
}
}
public class testTestDelegate {
public static void Main() { Name testName = new Name("Koani"); ShowValue showMethod = testName.DisplayToWindow;
showMethod();
}
}
用Action委托:
public class testTestDelegate
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = testName.DisplayToWindow;
showMethod();
}
}
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = testName.DisplayToWindow;
showMethod();
}
}
用Action+匿名委托
public class Anonymous
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = delegate() { testName.DisplayToWindow();} ;
showMethod();
}
}
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = delegate() { testName.DisplayToWindow();} ;
showMethod();
}
}
用Action+Lambda
public class LambdaExpression
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = () => testName.DisplayToWindow();
showMethod();
}
}
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = () => testName.DisplayToWindow();
showMethod();
}
}
那如果要传递的方法有参数怎么办,答案是用Action<T>
Action<T>有15个重载版本
同样Func<T>也有15个重载版本
View Code
public delegate void ShowValue();
public delegate void ShowValueName(string msg);
public class CustomObj
{
public void Display()
{
Console.Write("hello!");
Console.Read();
}
public void DisplayName(string name)
{
Console.Write("hello," + name);
Console.Read();
}
public delegate void ShowValueName(string msg);
public class CustomObj
{
public void Display()
{
Console.Write("hello!");
Console.Read();
}
public void DisplayName(string name)
{
Console.Write("hello," + name);
Console.Read();
}
View Code
using System;
using System.Collections.Generic;
using System.Text;
namespace ActionApp
{
class Program
{
static void Main(string[] args)
{
CustomObj obj = new CustomObj();
//ShowValue sv = obj.Display;
//sv();
Action a = obj.Display;
a();
//Action a = delegate { obj.Display(); };
//a();
//Action a = () => obj.Display();
//a();
//ShowValueName svn = obj.DisplayName;
//svn("dd");
//Action<string> at = obj.DisplayName;
//at("dd");
//Action<string> at = delegate(string s) { obj.DisplayName(s);};
//at("dd");
//Action<string> at = s => obj.DisplayName(s);
//at("dd");
}
}
using System.Collections.Generic;
using System.Text;
namespace ActionApp
{
class Program
{
static void Main(string[] args)
{
CustomObj obj = new CustomObj();
//ShowValue sv = obj.Display;
//sv();
Action a = obj.Display;
a();
//Action a = delegate { obj.Display(); };
//a();
//Action a = () => obj.Display();
//a();
//ShowValueName svn = obj.DisplayName;
//svn("dd");
//Action<string> at = obj.DisplayName;
//at("dd");
//Action<string> at = delegate(string s) { obj.DisplayName(s);};
//at("dd");
//Action<string> at = s => obj.DisplayName(s);
//at("dd");
}
}