|
查看: 1318|回复: 0
|
DOT NET 也有"CODE BLOCK"
[复制链接]
|
|
|
用过CLIPPER 的人材, 都知道CODE BLOCK 的厉害. DOT NET 也有"CODE BLOCK" 的味道,利用DELEGATE来实现. 下面就是一个简单的例子.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public delegate void Dosomething(int i); //declare code block signature
static void Main(string[] args)
{
List<Dosomething> tasks = new List<Dosomething>();
tasks.Add(DoactualThing1);
tasks.Add(DoactualThing2);
DoActualThing(new Dosomething(DoactualThing1));
DoActualThing(new Dosomething(DoactualThing2));
tasks.ForEach(DoActualThing);
tasks.ForEach(delegate(Dosomething task)
{
task.Invoke(1);
}
);
}
private static void DoActualThing(Dosomething task)
{
task.Invoke(1); //execute the code block
}
//concreate code 1
private static void DoactualThing1(int i)
{
Console.WriteLine("Hi folk...i doing first thing....");
}
//concreate code 2
private static void DoactualThing2(int i)
{
Console.WriteLine("Hi folk...i doing seconnd thing....");
}
}
} |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|