C#教程的代表和榜样

C# 委托允许像对象一样使用方法. This means that you are able to pass the method as an argument to some other method. This is done by pointing a delegate to a method and passing the delegate as an argument. Then using the delegate you are able to call the method that the delegate is pointing to.

The useful part of delegates is so a method can be passed from one object to another.

As we know a method represents some type of action that an object can do. That action is implemented in the method definition itself within the class the method is declared. With a delegate the implementation of the method or part of the method contained in the class, can be delegated to another class.

这里, we have a simple example to understand delegates. 我们有一个 Playlist 类,允许将歌曲添加到播放列表中. Playlist 类提供了一个名为 ShowPlaylist 的方法,可以打印出播放列表中的所有歌曲.

类播放列表
{
     // code eliminated for brevity
     
    public void ShowPlaylist()
    {
          foreach (歌曲在this.list中)
          {
               控制台.WriteLine("歌曲名称: {0}". 歌曲名称);       
          }
    }

}

使用 Playlist 类 ShowPlaylist 方法将打印完整的播放列表.

让我们向 Playlist 类引入代表. 使用委托将允许 Playlist 类的使用者实现显示播放列表的逻辑.

这里我们在 Playlist 类中声明一个委托,它有一个 Song 类型的参数

类播放列表
{
     // 为简洁起见删除了代码
     // delegate for showing song
     public delegate bool ProcessPlaylistDelegate(歌歌);

...

我们需要调整ShowPlayList() 使用 ProcessPlaylistDelegate 的方法. ShowPlayList方法将ProcessPlaylistDelegate作为参数. 现在我们可以在打印之前处理播放列表.

        //show the playlist using another function attached to ProcessPlaylistDelegate
        public void ShowPlaylist(处理播放列表委托处理播放列表)
        {
            foreach (歌曲在this.list中)
            {
                如果 (处理播放列表(歌曲))
                {
                    控制台.WriteLine("歌曲名称: {0}", 歌曲名称);
                }
            }
        }

现在,只要返回类型和参数类型与委托匹配,该类就可以将任何处理委托给另一个方法. Program 类下面使用 Playlist 类. 我们声明一个新的 Playlist.ProcessPlaylistDelegate,然后将 getMp3Songs 附加到该委托.

    班级计划
    {
        静态无效主要(细绳[] 参数)
        {
             播放列表 playlist1 = 新播放列表();

             //add some songs to the playlist

             //use the delegate to process the list before it is printed on the screen
             playlist1.ShowPlaylist(新的 Playlist.ProcessPlaylistDelegate(获取Mp3歌曲));
        }

  }

下面是 getMp3Songs 方法定义. 该方法仅返回mp3歌曲. 因此 ShowPlaylist 将只打印 mp3 歌曲.

        公共静态 bool GetMp3Songs(歌歌)
        {
            //more logic goes here
            return (歌曲.格式== "mp3");
        }

当我们可以以不同的方式处理播放列表而不必接触 Playlist 类时,委托的力量就会发挥作用. 下面我们声明 2 Program 类中的其他方法.

公共静态 bool GetCdSongs(歌歌)
 {
 //more logic goes here
 return (歌曲.格式== "光盘");
 }

公共静态 bool GetAllSongs(歌歌)
 {
 //more logic goes here
 return true;
 }

Now we just need to attach these to the delegates in the Main method. These methods will process the playlist differently. The first method will only include cd songs and the second method will include all the songs. This is just a simple example but we can add as much processing as we require without having to touch the class Playlist.

            playlist1.ShowPlaylist(新的 Playlist.ProcessPlaylistDelegate(GetCdSongs));

            playlist1.ShowPlaylist(新的 Playlist.ProcessPlaylistDelegate(GetAllSongs));


下载