AddFragment:如果允许,将声音片段添加到列表中。如果还没有 在列表中存在一个声音片段,其中identy-number等于idNr,一个新的声音碎片 创建(根据参数的值)并添加到列表中 并且在这种情况下返回值true。但是,如果确实存在了 列表中的声音片段,其identy-number等于idNr,该方法返回false。
public bool AddFragment (int _idNr, String _fileName, string _tile, int _duration)
{
foreach (SoundFragment fs3 in Fragments)
{
if (fs3.IdNr == __Idr ) ;
else
{
foreach (SoundFragment fs4 in Fragments)
{
if (fs4.IdNr != __Idr)
{
Fragments.Add(new SoundFragment(_idNr, _fileName, _tile, _duration));
}
return true;
}
}
return false;
答案 0 :(得分:1)
使用IEnumerable.Any扩展程序查看您的收藏集
public bool AddFragment (int _idNr, String _fileName, string _tile, int _duration)
{
bool added = false;
if(!Fragments.Any( x => x.IdNr == _idr))
{
Fragments.Add(new SoundFragment(_idNr, _fileName, _tile, _duration));
added = true;
}
return added;
}
答案 1 :(得分:0)
虽然我建议您使用之前发布的LINQ解决方案,但非LINQ解决方案是:
public bool AddFragment (int _idNr, String _fileName, string _tile, int _duration)
{
// Scan the list, looking for an existing fragment with this id number.
foreach (SoundFragment fs3 in Fragments)
{
if (fs3.IdNr == _idNr)
{
// found a matching fragment, so don't need to add.
return false;
}
}
// No matching fragment found. Add a new one.
Fragments.Add(new SoundFragment(....));
return true;
}