Array.Length 屬性(System) | Microsoft Docs
文章推薦指數: 80 %
它也會使用GetUpperBound 方法來判斷多維度陣列中每個維度中的專案數目。
C# 複製. 執行.
跳到主要內容
已不再支援此瀏覽器。
請升級至MicrosoftEdge,以利用最新功能、安全性更新和技術支援。
下載MicrosoftEdge
其他資訊
目錄
結束焦點模式
語言
閱讀英文
儲存
目錄
閱讀英文
儲存
編輯
Twitter
LinkedIn
Facebook
電子郵件
目錄
Array.Length屬性
參考
定義
命名空間:
System
組件:System.Runtime.dll
組件:mscorlib.dll
組件:netstandard.dll
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。
Microsoft對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
本文內容
取得Array所有維度的元素總數。
public:
propertyintLength{intget();};
publicintLength{get;}
memberthis.Length:int
PublicReadOnlyPropertyLengthAsInteger
屬性值
Int32
Array所有維度的項目總數;如果陣列中沒有項目,則為零。
例外狀況
OverflowException
陣列是多維度的,且包含超過Int32.MaxValue元素。
範例
下列範例會Length使用屬性來取得陣列中的元素總數。
它也會使用GetUpperBound方法來判斷多維度陣列中每個維度中的專案數目。
usingSystem;
publicclassExample
{
publicstaticvoidMain()
{
//Declareasingle-dimensionalstringarray
String[]array1d={"zero","one","two","three"};
ShowArrayInfo(array1d);
//Declareatwo-dimensionalstringarray
String[,]array2d={{"zero","0"},{"one","1"},
{"two","2"},{"three","3"},
{"four","4"},{"five","5"}};
ShowArrayInfo(array2d);
//Declareathree-dimensionalintegerarray
int[,,]array3d=newint[,,]{{{1,2,3},{4,5,6}},
{{7,8,9},{10,11,12}}};
ShowArrayInfo(array3d);
}
privatestaticvoidShowArrayInfo(Arrayarr)
{
Console.WriteLine("LengthofArray:{0,3}",arr.Length);
Console.WriteLine("NumberofDimensions:{0,3}",arr.Rank);
//Formultidimensionalarrays,shownumberofelementsineachdimension.
if(arr.Rank>1){
for(intdimension=1;dimension<=arr.Rank;dimension++)
Console.WriteLine("Dimension{0}:{1,3}",dimension,
arr.GetUpperBound(dimension-1)+1);
}
Console.WriteLine();
}
}
//Theexampledisplaysthefollowingoutput:
//LengthofArray:4
//NumberofDimensions:1
//
//LengthofArray:12
//NumberofDimensions:2
//Dimension1:6
//Dimension2:2
//
//LengthofArray:12
//NumberofDimensions:3
//Dimension1:2
//Dimension2:2
//Dimension3:3
openSystem
letshowArrayInfo(arr:Array)=
printfn$"LengthofArray:{arr.Length,3}"
printfn$"NumberofDimensions:{arr.Rank,3}"
//Formultidimensionalarrays,shownumberofelementsineachdimension.
ifarr.Rank>1then
fordimension=1toarr.Rankdo
printfn$"Dimension{dimension}:{arr.GetUpperBound(dimension-1)+1,3}"
printfn""
//Declareasingle-dimensionalstringarray
letarray1d=[|"zero";"one";"two";"three"|]
showArrayInfoarray1d
//Declareatwo-dimensionalstringarray
letarray2d=
array2D[["zero";"0"];["one";"1"]
["two";"2"];["three";"3"]
["four";"4"];["five";"5"]]
showArrayInfoarray2d
//Declareathree-dimensionalintegerarray
letarray3d=Array3D.create223"zero"
showArrayInfoarray3d
//Theexampledisplaysthefollowingoutput:
//LengthofArray:4
//NumberofDimensions:1
//
//LengthofArray:12
//NumberofDimensions:2
//Dimension1:6
//Dimension2:2
//
//LengthofArray:12
//NumberofDimensions:3
//Dimension1:2
//Dimension2:2
//Dimension3:3
ModuleExample
PublicSubMain()
'Declareasingle-dimensionalstringarray
Dimarray1d()AsString={"zero","one","two","three"}
ShowArrayInfo(array1d)
'Declareatwo-dimensionalstringarray
Dimarray2d(,)AsString={{"zero","0"},{"one","1"},
{"two","2"},{"three","3"},
{"four","4"},{"five","5"}}
ShowArrayInfo(array2d)
'Declareathree-dimensionalintegerarray
Dimarray3d(,,)AsInteger=NewInteger(,,){{{1,2,3},{4,5,6}},
{{7,8,9},{10,11,12}}}
ShowArrayInfo(array3d)
EndSub
PrivateSubShowArrayInfo(arrAsArray)
Console.WriteLine("LengthofArray:{0,3}",arr.Length)
Console.WriteLine("NumberofDimensions:{0,3}",arr.Rank)
'Formultidimensionalarrays,shownumberofelementsineachdimension.
Ifarr.Rank>1Then
FordimensionAsInteger=1Toarr.Rank
Console.WriteLine("Dimension{0}:{1,3}",dimension,
arr.GetUpperBound(dimension-1)+1)
Next
EndIf
Console.WriteLine()
EndSub
EndModule
'Theexampledisplaysthefollowingoutput:
'LengthofArray:4
'NumberofDimensions:1
'
'LengthofArray:12
'NumberofDimensions:2
'Dimension1:6
'Dimension2:2
'
'LengthofArray:12
'NumberofDimensions:3
'Dimension1:2
'Dimension2:2
'Dimension3:3
備註
擷取這個屬性的值是一種O(1)運算。
適用於
另請參閱
GetLength(Int32)
GetUpperBound(Int32)
GetLowerBound(Int32)
LongLength
Count
Rank
本文內容
延伸文章資訊
- 1Find length of an array in C# | Techie Delight
Alternatively, you can use the Array.GetLength() method to get the length of a single-dimensional...
- 2How to get the length of a multidimensional array in C# ...
To get the length of a multidimensional (row/column) array, we can use the Array.GetLength() meth...
- 3Array Size (Length) in C# - Stack Overflow
If it's a one-dimensional array a , a.Length. will give the number of elements of a . If b is a r...
- 4Array Class (System) | Microsoft Docs
- 5How 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 −.