请给我一个situtation这表明`need`的代表(或)函数指针 [英] Please show me a situtation which shows `need` for Delegates (or) function pointers

查看:198
本文介绍了请给我一个situtation这表明`need`的代表(或)函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会采取一类的代表和回调,以谁正在学习水平的程序员的学生。他们有基本的C / C ++和放大器; C#的背景。而不是直接表示如何使用它们。我想显示为什么函数指针?第一。我想从一个例子开始的情况,并要求他们你怎么会做这种?并让他们认识到需要的东西,然后把他们介绍给FunctionPointers,代表和放大器;回调。

I'm going take a class on "Delegates and Callbacks" to students who are learning level programmers. They have basic c/c++ & c# background. Instead of directly showing how to use them. I want to show "Why Function Pointers?" first. I want to start with an example situation and ask them "How will you do this"? and make them realize the need for something and then introduce them to FunctionPointers, Delegates & CallBacks.

所以,任何一个可以给我一个很好的例子,这表明需要在C / C ++ C#(或)函数指针代表。我不想例子中的事件处理GUI的例子,我不想示范如何使用委托有一种add2numbers的例子等。

So, Can any one show me a good example which shows the need for Delegates in C# (or) function pointers in C/C++. I don't want example of event handling in GUI example and I don't want demonstration of "How to use delegates" with an examples of kind add2numbers etc..

我在找什么实际的例子,他们能感觉到FunctionPointers,代表和放大器的需要;回调。

I'm looking for something practical example where they could feel the need of FunctionPointers, Delegates & CallBacks.

如果有什么好的文章,请把它们。

If there are any good articles, please post them.

推荐答案

您可以向他们展示过滤软件中的项目在几个地方的清单的例子。

You can show them an example of filtering a list of items in several places in your software.

例如,您可能有

public List<Person> GetMale(List<Person> people)
{
   List<Person> results = new List<Person>();
   foreach (Person p in people)
   {
       if (p.IsMale)
          results.Add(p);
   }
   return results;
}

public List<Person> GetFemale(List<Person> people)
{
   List<Person> results = new List<Person>();
   foreach (Person p in people)
   {
       if (!p.IsMale)
          results.Add(p);
   }
   return results;
}

要避免重蹈的foreach 迭代在每一个方法,你将要提取的实际情况(即 predicate 的本情况下),并把它落实在其他地方。

To avoid repeating the foreach iteration in every method, you will want to extract the actual condition (i.e. a predicate in this case), and have it implemented somewhere else.

所以,你将取代这两种方法有:

So you will replace these two methods with:

public List<Person> Filter(List<Person> people, Func<bool, Person> match)
{
   List<Person> results = new List<Person>();
   foreach (Person p in people)
   {
       if (match(p))
          results.Add(p);
   }
   return results;
}

然后调用它在你的code是这样的:

and then call it in your code like this:

List<Person> malePersons = Filter(people, p => p.IsMale);
List<Person> femalePersons = Filter(people, p => !p.IsMale);

请注意,实际的情况是,现在的迭代块外提取,并可以再利用同样的方法来创建你喜欢的任何自定义筛选逻辑。通过提取这个逻辑,你的委托问题给别人,让你的code松耦合。

Note that the actual condition is now extracted outside of the iterating block, and you can reuse the same method to create any custom filtering logic you like. By extracting this logic, you are delegating the problem to someone else, making your code loosely coupled.

使用C#2.0匿名方法的语法,调用此方法是这样的:

Using C# 2.0 anonymous method syntax, calling this method would look like this:

List<Person> malePersons = Filter(people, 
   delegate (Person p) { return p.IsMale; });
List<Person> femalePersons = Filter(people, 
   delegate (Person p) { return !p.IsMale; });

或实际使用方法:

or using actual methods:

List<Person> malePersons = Filter(people, MaleMatch);
List<Person> femalePersons = Filter(people, FemaleMatch);

其中predicates被定义为:

where predicates are defined as:

private bool MaleMatch(Person p)
{ 
   return p.IsMale;
}

private bool FemaleMatch(Person p)
{ 
   return !p.IsMale;
}

需要注意的是,我们都没有通过的的结果是很重要的这些方法,但实际的方法,指针,因此,当该方法被称为<$ C $内实际结果将返回C>过滤器方法。

It is important to note that we are not passing the result of these methods, but actual method "pointers", so actual results will be returned when the method is called inside the Filter method.

另请注意, LINQ在.NET 3.5中已经包含了其中,的扩展,它使用委托的情况下,投影和其他的东西很多其他的方法,所以你基本上只需要传递的委托方法,它确实喜欢这个例子中,同样的事情,适当的签名。

Note also that LINQ in .Net 3.5 already contains a Where extension method which does the same thing like this example, and many other methods which use delegates for conditions, projecting and other stuff, so you basically only need to pass a delegate with the appropriate signature.

这篇关于请给我一个situtation这表明`need`的代表(或)函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆