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; } } |
public class Program { public delegate void MyDelegate(); 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(delegate(string sItem) { if (sItem[0] == ''A'') return true; else return false; }); Console.WriteLine("----- Strings starting with letter ''A'' -----"); foreach (string s in AStrings) { Console.WriteLine(s); } //获得集合中以字母’ T ‘开头的字符项数组 string[] TStrings = objMyCol.GetFilteredItemArray(delegate(string sItem) { if (sItem[0] == ''T'') return true; else return false; }); Console.WriteLine("----- Strings starting with letter ''T'' -----"); foreach (string s in TStrings) { Console.WriteLine(s); } } } |
public class Program { public delegate void MyDelegate(); public static void Main(string[] args) { //实例数据成员测试 Program p = new Program(); for(int i=1;i<=5;i++) p.TestInstanceDataMembers(); } public void TestInstanceDataMembers() { MyDelegate d = delegate { Console.WriteLine("Count: {0}",++m_iCount); }; d(); } public int m_iCount = 0; } |
public class Program { public delegate void MyDelegate(); public static void Main(string[] args) { int iTemp = 100; MyDelegate dlg = delegate { Console.WriteLine(iTemp); }; dlg(); } } |
public class Program { private class InnerClass { private void InstanceMethod() { Console.WriteLine(iTemp); } public int iTemp; } public delegate void MyDelegate(); public static void Main(string[] args) { InnerClass localObject = new InnerClass(); localObject.iTemp = 100; MyDelegate dlg = new MyDelegate(localObject.InstanceMethod); dlg(); } } |
public class Program { public delegate void MyDelegate(); public static void Main(string[] args) { MyDelegate dlg = null; int iTemp = 100; if (iTemp > 50) { int jTemp = 200; dlg = delegate { Console.WriteLine("iTemp: {0}, jTemp: {1}",iTemp,jTemp); }; } dlg(); } } |
public class Program { //包装来自外部作用域的局部变量''iTemp''的类 private class InnerClassScope1 { public int iTemp; } //包装来自内部作用域和匿名方法的局部变量的类 private class InnerClassScope2 { public void InstanceMethod() { Console.WriteLine("iTemp: {0}, jTemp: {1}", localObjectScope1.iTemp, jTemp); } public InnerClassScope1 localObjectScope1; public int jTemp; } public delegate void MyDelegate(); public static void Main(string[] args) { MyDelegate dlg = null; InnerClassScope1 localObject1 = new InnerClassScope1(); localObject1.iTemp = 100; if (localObject1.iTemp > 50) { InnerClassScope2 localObject2 = new InnerClassScope2(); localObject2.localObjectScope1 = localObject1; localObject2.jTemp = 200; dlg = new MyDelegate(localObject2.InstanceMethod); } dlg(); } } |
public class Program { public delegate void MyDelegate(); public static void Main(string[] args) { MyDelegate d = null; for (int i = 1; i <= 5; i++) { MyDelegate tempD = delegate { Console.WriteLine(i); }; d += tempD; } d(); } } |
public class Program { public delegate void MyDelegate(); public static void Main(string[] args) { MyDelegate d = null; for (int i = 1; i <= 5; i++) { int k = i; MyDelegate tempD = delegate { Console.WriteLine(k); }; d += tempD; } d(); } } |
欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛
https://bbs.txwb.com
关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩
|
本文来源:vczx 作者:佚名