找到 2628 篇文章,关于 C#
80 次浏览
要获取数组指定维度中的元素总数,代码如下:示例 在线演示using System; public class Demo { public static void Main() { string[] products = new string[] { "Andy", "Mark", "Gary", "Andre"}; Console.WriteLine("一个或多个名称以 'A' 开头?= {0}", Array.Exists(products, ele => ele.StartsWith("A"))); Console.WriteLine("数组是否具有固定大小?= " + products.IsFixedSize); Console.WriteLine("数组是否只读?= " + products.IsReadOnly); Console.WriteLine("数组是否同步?= " + products.IsSynchronized); ... 阅读更多
95 次浏览
要将 SortedList 对象的容量设置为实际元素数量,代码如下:示例 在线演示using System; using System.Collections; public class Demo { public static void Main(String[] args) { SortedList sortedList = new SortedList(); sortedList.Add("A", "1"); sortedList.Add("B", "2"); sortedList.Add("C", "3"); sortedList.Add("D", "4"); sortedList.Add("E", "5"); sortedList.Add("F", "6"); sortedList.Add("G", "7"); sortedList.Add("H", "8"); sortedList.Add("I", "9"); sortedList.Add("J", "10"); Console.WriteLine("SortedList 元素..."); ... 阅读更多
94 次浏览
要将 ArrayList 的容量设置为实际元素数量,代码如下:示例 在线演示using System; using System.Collections; public class Demo { public static void Main(String[] args) { ArrayList list1 = new ArrayList(); list1.Add("A"); list1.Add("B"); list1.Add("C"); list1.Add("D"); list1.Add("E"); list1.Add("F"); list1.Add("G"); list1.Add("H"); list1.Add("I"); Console.WriteLine("ArrayList1 中的元素..."); foreach (string res in list1) { Console.WriteLine(res); ... 阅读更多
160 次浏览
要搜索满足条件的元素,并返回整个 List 中最后一个匹配项的从零开始的索引,代码如下:示例 在线演示using System; using System.Collections.Generic; public class Demo { private static bool demo(int i) { return ((i % 10) == 0); } public static void Main(String[] args) { List list = new List(); list.Add(200); list.Add(215); list.Add(310); list.Add(500); list.Add(600); Console.WriteLine("List 元素..."); foreach (int i in ... 阅读更多
114 次浏览
要搜索集合中指定对象的索引,代码如下:示例 在线演示using System; using System.Collections.Specialized; public class Demo { public static void Main() { StringCollection strCol = new StringCollection(); strCol.Add("Accessories"); strCol.Add("Books"); strCol.Add("Electronics"); strCol.Add("Books"); Console.WriteLine("StringCollection 元素..."); foreach (string res in strCol) { Console.WriteLine(res); } strCol.Insert(2, "Headphone"); Console.WriteLine("StringCollection 元素...已更新"); foreach (string res in strCol) { Console.WriteLine(res); ... 阅读更多
65 次浏览
要获取 BitArray 中包含的元素数量,代码如下:示例 在线演示using System; using System.Collections; public class Demo { public static void Main() { BitArray arr1 = new BitArray(5); BitArray arr2 = new BitArray(5); arr1[0] = false; arr1[1] = false; arr2[0] = false; arr2[1] = true; Console.WriteLine("BitArray1 元素..."); foreach (bool res in arr1) { Console.WriteLine(res); } Console.WriteLine("BitArray2 元素..."); ... 阅读更多
477 次浏览
要检查线程是否处于活动状态,代码如下:示例 在线演示using System; using System.Threading; public class Demo { public static void Main() { Thread thread = new Thread(new ThreadStart(demo1)); thread = Thread.CurrentThread; Console.WriteLine("线程是否处于活动状态?= "+thread.IsAlive); ThreadPool.QueueUserWorkItem(new WaitCallback(demo2)); Console.WriteLine("线程的当前状态 = "+thread.ThreadState); Console.WriteLine("ManagedThreadId = "+thread.ManagedThreadId); thread.IsBackground = true; Console.WriteLine("线程是否为后台线程?= "+thread.IsBackground); } public static void demo1() { ... 阅读更多
132 次浏览
要搜索 SortedList 对象,代码如下:示例 在线演示using System; using System.Collections; public class Demo { public static void Main() { SortedList list = new SortedList(); list.Add("1", "One"); list.Add("2", "Two"); list.Add("3", "Three"); list.Add("4", "Four"); list.Add("5", "Five"); list.Add("6", "Six"); list.Add("7", "Seven"); list.Add("8", "Eight"); Console.WriteLine("SortedList 的键和值...."); foreach(DictionaryEntry k in list ) Console.WriteLine("键: {0}, 值: {1}", k.Key , k.Value ... 阅读更多
190 次浏览
要检查线程是否为后台线程,代码如下:示例 在线演示using System; using System.Threading; public class Demo { public static void Main() { Thread thread = new Thread(new ThreadStart(demo1)); ThreadPool.QueueUserWorkItem(new WaitCallback(demo2)); Console.WriteLine("线程的当前状态 = "+thread.ThreadState); Console.WriteLine("ManagedThreadId = "+thread.ManagedThreadId); Console.WriteLine("线程是否为后台线程?= "+Thread.CurrentThread.IsBackground); } public static void demo1() { Thread.Sleep(2000); } public static void demo2(object stateInfo) { Console.WriteLine("线程属于托管线程 ... 阅读更多
179 次浏览
要获取 C# 中 HashSet 中的元素数量,代码如下:示例 在线演示using System; using System.Collections.Generic; public class Demo { public static void Main() { HashSet set1 = new HashSet(); set1.Add(25); set1.Add(50); set1.Add(75); set1.Add(100); set1.Add(125); set1.Add(150); Console.WriteLine("HashSet1 中的元素"); foreach(int val in set1) { Console.WriteLine(val); } Console.WriteLine("HashSet1 中的元素数量 = "+set1.Count); HashSet set2 ... 阅读更多