C#返回在字典或列表中指定的序列中满足条件的元素数量 dictionary.count

来源:互联网 发布:视频提取软件安卓版 编辑:程序博客网 时间:2024/04/30 07:54
例如字典重载的方法:Dictionary.Count方法
Count<KeyValuePair<TKey, TValue>>(Func<KeyValuePair<TKey, TValue>, Boolean>)已重载。 返回一个数字,表示在指定的序列中满足条件的元素数量。 (由 Enumerable 定义。)
class Pet            {                public string Name { get; set; }                public bool Vaccinated { get; set; }            }            public static void CountEx2()            {                Pet[] pets = { new Pet { Name="Barley", Vaccinated=true },                               new Pet { Name="Boots", Vaccinated=false },                               new Pet { Name="Whiskers", Vaccinated=false } };                try                {                    int numberUnvaccinated = pets.Count(p => p.Vaccinated == false);                    Console.WriteLine("There are {0} unvaccinated animals.", numberUnvaccinated);                }                catch (OverflowException)                {                    Console.WriteLine("The count is too large to store as an Int32.");                    Console.WriteLine("Try using the LongCount() method instead.");                }            }
0 0
原创粉丝点击