C#列表继承

0 c# inheritance list

我正在写一个处理歌曲的C#程序.我有一个Song类和一个SongCollection类,它继承了Song类型的列表.我需要编写一个以艺术家(字符串)作为参数的方法,我需要返回该艺术家的新SongCollection.我在访问歌曲列表时遇到了一些困难.

public SongCollection GetAllByArtist(string artist)
{
        SongCollection newSongs = new SongCollection();
        if (this.Artist == artist)
        {
            newSongs.Add(this.Song);
        }
        return newSongs;
}
Run Code Online (Sandbox Code Playgroud)

Uri*_*iil 5

这应该适合你:

 public SongCollection GetAllByArtist(string artist)
 {
    SongCollection newSongs = new SongCollection();
    newSongs.AddRange(this.Where(p=>p.Artist == artist))        
    return newSongs;
 }
Run Code Online (Sandbox Code Playgroud)