List<> and
ArrayList Class Diagrams
Using the Bit Complement of the
BinarySearch() Result
代码
1using System; 2using System.Collections.Generic; 3class Program 4{ 5 static void Main() 6 { 7 List<string> list = new List<string>(); 8 int search; 910 list.Add("public");11 list.Add("protected");12 list.Add("private");1314 list.Sort();1516 search = list.BinarySearch("protected internal");17 if (search < 0)18 {19 /**//*The bitwise complement (~) of this value is 20 * the index of the next element larger than the 21 * element being sought, or the total element count 22 * if there is no greater value. This provides a 23 * convenient means to insert new values into the 24 * list at the specific location so as to maintain sorting */25 list.Insert(~search, "protected internal");26 }2728 foreach (string accessModifier in list)29 {30 Console.WriteLine(accessModifier);31 }32 Console.ReadKey();33 }34}
Demonstrating
FindAll() and Its Predicate Parameter
代码
1using System; 2using System.Collections.Generic; 3using Microsoft.VisualStudio.TestTools.UnitTesting; 4 5class Program 6{ 7 8 static void Main() 9 {10 List<int> list = new List<int>();11 list.Add(1);12 list.Add(2);13 list.Add(3);14 list.Add(2);1516 List<int> results = list.FindAll(Even);17 /**//*18 * 命名空间:Microsoft.VisualStudio.TestTools.UnitTesting19 * 程序集:Microsoft.VisualStudio.QualityTools.UnitTestFramework(在 microsoft.visualstudio.qualitytools.unittestframework.dll 中)20 */21 Assert.AreEqual(2, results.Count);22 Assert.IsTrue(results.Contains(2));23 Assert.IsFalse(results.Contains(3));24 }25 public static bool Even(int value)26 {27 if ((value % 2) == 0)28 {29 return true;30 }31 else32 {33 return false;34 }35 }36}
转载于:https://www.cnblogs.com/nanshouyong326/archive/2007/05/10/741889.html