Array.GetLowerBound 메서드
2013-03-11
Array에 있는 지정한 차원의 하한을 가져옵니다.
어셈블리: mscorlib(mscorlib.dll)
매개 변수
- dimension
- 형식: System.Int32
하한을 지정해야 하는 Array의 0부터 시작하는 차원입니다.
| 예외 | 조건 |
|---|---|
| IndexOutOfRangeException | dimension이 0보다 작은 경우 -또는- dimension이 Rank보다 크거나 같은 경우 |
다음 코드 예제에서는 GetLowerBound 및 GetUpperBound를 사용하여 1차원 배열과 다차원 배열을 초기화합니다.
참고: |
|---|
이 예제를 실행하려면 Windows Phone용 정적 TextBlock 컨트롤이 있는 예제 빌드를 참조하세요. |
using System; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Creates a new one-dimensional Array of type Int32. Array my1DIntArray = Array.CreateInstance(typeof(Int32), 5); // Uses GetLowerBound and GetUpperBound in the for loop. for (int i = my1DIntArray.GetLowerBound(0); i <= my1DIntArray.GetUpperBound(0); i++) my1DIntArray.SetValue(i + 1, i); // Displays the bounds and values of the one-dimensional Array. outputBlock.Text += "One-dimensional Array:" + "\n"; outputBlock.Text += "Rank\tLower\tUpper" + "\n"; outputBlock.Text += String.Format("{0}\t{1}\t{2}", 0, my1DIntArray.GetLowerBound(0), my1DIntArray.GetUpperBound(0)) + "\n"; outputBlock.Text += "Values:" + "\n"; PrintValues(outputBlock, my1DIntArray); outputBlock.Text += "\n"; // Creates a new three-dimensional Array of type Int32. Array my3DIntArray = Array.CreateInstance(typeof(Int32), 2, 3, 4); // Uses GetLowerBound and GetUpperBound in the for loop. for (int i = my3DIntArray.GetLowerBound(0); i <= my3DIntArray.GetUpperBound(0); i++) for (int j = my3DIntArray.GetLowerBound(1); j <= my3DIntArray.GetUpperBound(1); j++) for (int k = my3DIntArray.GetLowerBound(2); k <= my3DIntArray.GetUpperBound(2); k++) { my3DIntArray.SetValue((i * 100) + (j * 10) + k, i, j, k); } // Displays the bounds and values of the multidimensional Array. outputBlock.Text += "Multidimensional Array:" + "\n"; outputBlock.Text += "Rank\tLower\tUpper" + "\n"; for (int i = 0; i < my3DIntArray.Rank; i++) outputBlock.Text += String.Format("{0}\t{1}\t{2}", i, my3DIntArray.GetLowerBound(i), my3DIntArray.GetUpperBound(i)) + "\n"; outputBlock.Text += "Values:" + "\n"; PrintValues(outputBlock, my3DIntArray); } public static void PrintValues(System.Windows.Controls.TextBlock outputBlock, Array myArr) { System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator(); int i = 0; int cols = myArr.GetLength(myArr.Rank - 1); while (myEnumerator.MoveNext()) { if (i < cols) { i++; } else { outputBlock.Text += "\n"; i = 1; } outputBlock.Text += String.Format("\t{0}", myEnumerator.Current); } outputBlock.Text += "\n"; } } /* This code produces the following output. One-dimensional Array: Rank Lower Upper 0 0 4 Values: 1 2 3 4 5 Multidimensional Array: Rank Lower Upper 0 0 1 1 0 2 2 0 3 Values: 0 1 2 3 10 11 12 13 20 21 22 23 100 101 102 103 110 111 112 113 120 121 122 123 */
참고: