這篇文章是在介紹ASP.NET程式進行委托的方式
1.Action<T>: 無傳回值的委托
2.Func<T>: 可傳回值的委托
3.delegate: 委托
4.匿名委托
5.lambda表達式
在下面的 clsDeletgateTest 類中都有使用到, 請自行參考!
using System;
namespace dummyTest
{
class clsDeletgateTest
{
/// <summary>
/// 委托測試
/// Action<T>: 無傳回值的委托
/// Func<T>: 可傳回值的委托
/// </summary>
public class DeletgateExample1
{
//製作一個委托的方法
public static void ActionTest<T>(Action<T> action, T p) { action(p); }
public static void ActionTest<T>(Action<T, T> action, T p1, T p2) { action(p1, p2); }
public static string FuncTest<T>(Func<T, string> action, T p) { return action(p); }
public static string FuncTest<T>(Func<T, T, string> action, T p1, T p2) { return action(p1, p2); }
public DeletgateExample1() {
ActionTest<string>(MethonInStringOutVoid, "Action delegate test."); // 無法傳回值
ActionTest<string>(MethonInStringOutVoid, "Action ", "delegate test."); // 無法傳回值
string ret1 = FuncTest<string>(MethonInStringOutString, "Func delegate test."); // 有傳回值
string ret2 = FuncTest<string>(MethonInStringOutString, "Func ", "delegate test."); // 有傳回值
Console.WriteLine("ret1: {0}", ret1);
Console.WriteLine("ret2: {0}", ret2);
}
public void MethonInStringOutVoid(string s) {
Console.WriteLine(s);
}
public void MethonInStringOutVoid(string s1, string s2) {
Console.WriteLine(s1 + s2);
}
public string MethonInStringOutString(string s) {
Console.WriteLine(s);
return "Return: " + s;
}
public string MethonInStringOutString(string s1, string s2) {
Console.WriteLine(s1 + s2);
return "Return: " + s1 + s2;
}
}
/// <summary>
/// delegate C# 1.0, C# 2.0, C# 3.0
/// </summary>
public class DeletgateExample2
{
delegate void TestDelegateWithString(string s);
delegate void TestDelegateWithDouble(double d);
public static void MethonInStringOutVoid(string s) {
Console.WriteLine(s);
}
public DeletgateExample2() {
TestDelegateWithString testDelA1 = MethonInStringOutVoid;
//C# 1.0:
TestDelegateWithString testDelA2 = new TestDelegateWithString(MethonInStringOutVoid);
//C# 2.0:
//內聯代碼,稱為“匿名方法”。 這個方法將一個字符串作為輸入參數。
TestDelegateWithString testDelB = delegate (string s) { Console.WriteLine(s); };
//C# 3.0:
//一個lambda表達式。 lambda也需要一個字符串作為輸入參數(x)。 x的類型由編譯器推斷。
TestDelegateWithString testDelC1 = (x) => { Console.WriteLine(x.ToString()); };
TestDelegateWithDouble testDelC2 = (x) => { Console.WriteLine(x.ToString()); };
// Invoke the delegates.
testDelA1("Hello. My name is M and I write lines.");
testDelA2("Hello. My name is M and I write lines.");
testDelB("That's nothing. I'm anonymous and ");
testDelC1("I'm a famous author.");
testDelC2(123.456);
}
}
/// <summary>
/// delegate 匿名方法 + lambda
/// https://dotblogs.com.tw/lastsecret/2010/06/26/16201
/// </summary>
public class DeletgateExample3
{
public delegate int TestDelegate(int x, int y);
public static int 加法(int x, int y) {
return (x + y);
}
public DeletgateExample3() {
TestDelegate d;
string s = "+";
switch(s)
{
case "+":
d = delegate (int x, int y) { return x + y; };
break;
case "-":
d = (int x, int y) => { return x - y; };
break;
case "*":
d = (int x, int y) => x * y;
break;
case "/":
d = (x, y) => y != 0 ? x / y : 0;
break;
default:
d = new TestDelegate(加法);
break;
}
}
}
static void Main(string[] args) {
DeletgateExample1 d1 = new DeletgateExample1();
DeletgateExample2 d2 = new DeletgateExample2();
}
}
}
留言列表