扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
public class MyCollection { public delegate bool SelectItem(string sItem); public string[] GetFilteredItemArray(SelectItem itemFilter) { List<string> sList = new List<string>(); foreach(string sItem in m_sList) { if (itemFilter(sItem) == true) sList.Add(sItem); } return sList.ToArray(); } public List<string> ItemList { get { return m_sList; } } private List<string> m_sList = new List<string>(); } |
public class Program { public static void Main(string[] args) { MyCollection objMyCol = new MyCollection(); objMyCol.ItemList.Add("Aditya"); objMyCol.ItemList.Add("Tanu"); objMyCol.ItemList.Add("Manoj"); objMyCol.ItemList.Add("Ahan"); objMyCol.ItemList.Add("Hasi"); //获得集合中以字母’A‘开头的字符项数组 string[] AStrings = objMyCol.GetFilteredItemArray(FilterStringWithA); Console.WriteLine("----- Strings starting with letter ''A'' -----"); foreach(string s in AStrings) { Console.WriteLine(s); } //获得集合中以字母’T‘开头的字符项数组 string[] TStrings = objMyCol.GetFilteredItemArray(FilterStringWithT); Console.WriteLine("----- Strings starting with letter ''T'' -----"); foreach(string s in TStrings) { Console.WriteLine(s); } } public static bool FilterStringWithA(string sItem) { if (sItem[0] == ''A'') return true; else return false; } public static bool FilterStringWithT(string sItem) { if (sItem[0] == ''T'') return true; else return false; } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。