Find length of an array in C# | Techie Delight
文章推薦指數: 80 %
Alternatively, you can use the Array.GetLength() method to get the length of a single-dimensional array. The idea is to pass the zero dimension to the GetLength ... Skiptocontent ThispostwilldiscusshowtofindthelengthofanarrayinC#. 1.UsingArray.LengthProperty ThestandardsolutiontofindthelengthofanarrayisusingtheArray.Lengthproperty.Itreturnsthetotalnumberofelementscontainedinanarray.Ifthearrayisempty,itreturnszero.Thefollowingexampledemonstratesit: 123456789101112 usingSystem; publicclassExample{ publicstaticvoidMain() { int[]array={1,2,3,4,5}; intlength=array.Length; Console.WriteLine("Lengthis"+length); }} Download RunCode Output: Lengthis5 Formulti-dimensionalarrays,theArray.Lengthpropertyreturnsthetotalnumberofelementsinallthedimensions.Inotherwords,itreturnsthesumofthetotalnumberofelementsineachdimensionofamultidimensionalarray. 123456789101112 usingSystem; publicclassExample{ publicstaticvoidMain() { int[,]array=newint[2,3]{{1,2,3},{4,5,6}}; intlength=array.Length; Console.WriteLine("Lengthis"+length); }} Download RunCode Output: Lengthis6 InC#,ajaggedarraycanbeofdifferentdimensionsandsizes.Forajaggedarray,theLengthpropertywillindicatethenumberofdimensionsinthearray.Forexample, 1234567891011121314151617 usingSystem; publicclassExample{ publicstaticvoidMain() { int[][]array=newint[3][]; array[0]=newint[]{1,2}; array[1]=newint[]{3,4,5}; array[2]=newint[]{6,7,8,9}; Console.WriteLine("array.Lengthis"+array.Length); Console.WriteLine("array[0].Lengthis"+array[0].Length); Console.WriteLine("array[1].Lengthis"+array[1].Length); Console.WriteLine("array[2].Lengthis"+array[2].Length); }} Download RunCode Output: array.Lengthis3 array[0].Lengthis2 array[1].Lengthis3 array[2].Lengthis4 2.UsingArray.GetLength(Int32)Method Alternatively,youcanusetheArray.GetLength()methodtogetthelengthofasingle-dimensionalarray.TheideaistopassthezerodimensiontotheGetLengthmethodtodeterminethetotalnumberofelementsinthefirstdimensionofthearray. 123456789101112 usingSystem; publicclassExample{ publicstaticvoidMain() { int[]array={1,2,3,4,5}; intlength=array.GetLength(0); Console.WriteLine("Lengthis"+length); }} Download RunCode Output: Lengthis5 Ifthearrayismultidimensional,theArray.GetLength()methodreturnsthetotalnumberofelementsinthespecifieddimensionofthemultidimensionalarray. 123456789101112 usingSystem; publicclassExample{ publicstaticvoidMain() { int[,]array=newint[2,3]{{1,2,3},{4,5,6}}; intlength=array.GetLength(0); Console.WriteLine("Lengthis"+length); }} Download RunCode Output: Lengthis2 3.UsingArray.RankProperty Ifyouneedthenumberofdimensionsofanarrayinsteadofthenumberofelements,useArray.RankProperty.Itreturns1foraone-dimensionalarrayandjaggedarray(anarrayofarrays),2foratwo-dimensionalarray,andsoon. 123456789101112 usingSystem; publicclassExample{ publicstaticvoidMain() { int[,]array=newint[3,5]; intrank=array.Rank; Console.WriteLine("Rankis"+rank); }} Download RunCode Output: Rankis2 4.Usingforeach Anotherpossible,butlessrecommended,optionistomanuallycountthenumberofelementsinthearray.Thefollowingexampleshowshowtouseaforeachloopandacountervariabletoachievethesame. 12345678910111213141516 usingSystem; publicclassExample{ publicstaticvoidMain() { int[]array={1,2,3,4,5}; intlength=0; foreach(variteminarray){ length++; } Console.WriteLine("Lengthis"+length); }} Download RunCode Output: Lengthis5 That’sallaboutfindingthelengthofanarrayinC#. RatethispostSubmitRatingAveragerating5/5.Votecount:12Novotessofar!Bethefirsttoratethispost.Wearesorrythatthispostwasnotusefulforyou!Tellushowwecanimprovethispost?SubmitFeedback Thanksforreading. PleaseuseouronlinecompilertopostcodeincommentsusingC,C++,Java,Python,JavaScript,C#,PHP,andmanymorepopularprogramminglanguages. Likeus?Referustoyourfriendsandhelpusgrow.Happycoding🙂 Subscribe Notifyof newfollow-upcomments newrepliestomycomments Name* Email* Name* Email* 0Comments InlineFeedbacks Viewallcomments LoadMoreComments BrowseAlgorithm Amazon Beginner BinarySearch BitHacks Bottom-up Breadth-firstsearch Depth-firstsearch Easy FIFO Greedy Hard Hashing LIFO Maze Medium Microsoft MustKnow PriorityQueue Recursive SlidingWindow Top-down Trie Subscribetonewposts Enteryouremailaddresstosubscribetonewposts. EmailAddress Subscribe Thiswebsiteusescookies.Byusingthissiteyouagreetotheuseofcookies,ourpolicies,copyrighttermsandotherconditions.Readour PrivacyPolicy. DoNOTfollowthislinkoryouwillbebannedfromthesite! Insert
延伸文章資訊
- 1C# Array.Length屬性代碼示例- 純淨天空
C# Array.Length屬性代碼示例,System.Array.Length用法.
- 2Working With Arrays In C#
- 3Find length of an array in C# | Techie Delight
Alternatively, you can use the Array.GetLength() method to get the length of a single-dimensional...
- 4How do you find the length of an array in C#? - Tutorialspoint
To find the length of an array, use the Array.Length() method. Example. Let us see an example −.
- 5Calculate length of array in C – 2 Code Examples - Interview Sansar