How to count elements in C# array? - TutorialsTeacher

文章推薦指數: 80 %
投票人數:10人

How to count elements in C# array? ... You can count the total number of elements or some specific elements in the array using an extension method ... C# ASP.NETCore MVC IoC TypeScript Angular Python SQLServer MongoDB More ✕ .NETTutorials C# ASP.NETCore ASP.NETMVC IoC webapi LINQ ClientSide JavaScript jQuery Node.js D3.js TypeScript Angular11 AngularJS1 Sass ServerSide https Python SQL SQLServer PostgreSQL MongoDB SkillTests ASP.NETCore ASP.NETMVC LINQ C# webapi IoC TypeScript AngularJS Node.js jQuery JavaScript Articles Tests C#Articles Asynchronousprogrammingwithasync,await,TaskinC# Differencebetweenstatic,readonly,andconstantinC# IndexOutOfRangeExceptioninC# ForeachLoopinC# HowtoloopthroughanenuminC#? NullReferenceExceptioninC# GenerateRandomNumbersinC# SetDefaultValuetoPropertyinC# VariableScopesinC# WhentouseStructoverClassinC# DifferencebetweenTwoDatesinC# ConvertinttoenuminC# BigIntegerDataTypeinC# ConvertStringtoEnuminC# ConvertanObjecttoJSONinC# ConvertJSONStringtoObjectinC# TheMain()MethodinC# HowtoPassorAccessCommand-lineArgumentsinC#? DateTimeFormatsinC# HowtoconvertdateobjecttostringinC#? SearchinginC#array ComparestringsinC# HowtocountelementsinC#array? HowtocombinetwoarrayswithoutduplicatevaluesinC#? DifferencebetweenStringandstringinC#. HowtogetacommaseparatedstringfromanarrayinC#? HowtoremoveduplicatevaluesfromanarrayinC#? HowtosortanarrayinC#? HowtosortobjectarraybyspecificpropertyinC#? refKeywordinC# Querygeolocation&proxydatain.NETusingIP2Location BoxingandUnboxinginC# outkeywordinC# HowtoconvertstringtointinC#? DesignPrinciplevsDesignPattern HowtocalculatethecodeexecutiontimeinC#? HowtoreadfileusingStreamReaderinC#? DifferencebetweendelegatesandeventsinC# HowtosortthegenericSortedListinthedescendingorder? HowtowritefileusingStreamWriterinC#? DifferencebetweenArrayandArrayList DifferencebetweenHashtableandDictionary HowtocountelementsinC#array? C# By TutorialsTeacher  22Jan2020 YoucancountthetotalnumberofelementsorsomespecificelementsinthearrayusinganextensionmethodCount()method. TheCount()methodisanextensionmethodofIEnumerableincludedinSystem.Linq.Enumerableclass. ItcanbeusedwithanycollectionoracustomclassthatimplementsIEnumerableinterface.Allthebuilt-incollectionsinC#,suchasarray,ArrayList,List,Dictionary,SortedList,etc.implementsIEnumerable,andsotheCount()methodcanbeusedwiththesecollectiontypes. Count()Overloads Count() Returnstotalnumberofelementsinanarray. Count(Func) ReturnsthetotalnumberofelementsinanarraythatmatchesthespecifiedconditionusingFuncdelegate. Thefollowingexampledisplaysthetotalnumberofelementsinthearray. Example:CountArrayElementsCopy string[]empty=newstring[5]; vartotalElements=empty.Count();//5 string[]animals={"Cat","Alligator","fox","donkey","Cat","alligator"}; totalElements=animals.Count();//6 int[]nums={1,2,3,4,3,55,23,2,5,6,2,2}; totalElements=nums.Count();//12 Tryit Intheaboveexample,theempty.Count()returns5,eveniftherearenoelementsinthearray.Thisisbecauseanarrayalreadyhasfivenullelements.Forothers,itwillreturnthetotalnumberofelements. CountSpecificElementsinanArray Thefollowingexampleshowshowtocountthespecificelementsbasedonsomecondition. Example:CountSpecificElementsCopy string[]animals={"Cat","Alligator","fox","donkey","Cat","alligator"}; vartotalCats=animals.Count(s=>s=="Cat"); varanimalsStartsWithA=animals1.Count(s=>s.StartsWith("a",StringComparison.CurrentCultureIgnoreCase)); int[]nums={1,2,3,4,3,55,23,2,5,6,2,2}; vartotalEvenNums=nums.Count(n=>n%2==0); Tryit YoucanalsouseRegexwiththeCount()method,asshownbelow. Example:RegexwithCount()Copy string[]animals={"Cat","Alligator","fox","donkey","Cat","alligator"}; varanimalsWithCapitalLetter=animals.Count(s=> { returnRegex.IsMatch(s,"^[A-Z]"); }); Tryit RelatedArticles Asynchronousprogrammingwithasync,await,TaskinC# HowtoloopthroughanenuminC#? GenerateRandomNumbersinC# DifferencebetweenTwoDatesinC# ConvertinttoenuminC# BigIntegerDataTypeinC# ConvertStringtoEnuminC# ConvertanObjecttoJSONinC# ConvertJSONStringtoObjectinC# DateTimeFormatsinC# HowtoconvertdateobjecttostringinC#? SearchinginC#array ComparestringsinC# HowtocombinetwoarrayswithoutduplicatevaluesinC#? DifferencebetweenStringandstringinC#. HowtogetacommaseparatedstringfromanarrayinC#? HowtoremoveduplicatevaluesfromanarrayinC#? HowtosortanarrayinC#? HowtosortobjectarraybyspecificpropertyinC#? BoxingandUnboxinginC# HowtoconvertstringtointinC#? DifferencebetweenArrayandArrayList TutorialsTeacher Author tutorialsteacher.comisafreeself-learningtechnologywebsiteforbeginnersandprofessionals. Share Tweet Share Whatsapp ArticleTags All C# MVC WebAPI Azure IIS JavaScript Node.js Python SQLServer SEO Entrepreneur Productivity



請為這篇文章評分?