What is the difference between these two methods and when would you use one instead of the other?
int[,] array = new int[4,3];
int length0 = array.GetLength(0);
int upperbound0 = array.GetUpperBound(0);
MSDN says that GetLength return the number of elements where as GetUpperBound determine the max index, but how could this be different since arrays are initialized with elements for each index?
Take a look at this (rarely used) method. From Docs:
With it, you can create an array with indices from
-5 ... +5. If you ever use this kind of array, thenGetUpperBound()suddenly becomes a lot more useful thanGetLength()-1. There also exists aGetLowerBound().But the C# support for this kind of arrays is low, you cannot use
[]. You would only need those methods in combination with the Array.GetValue() and SetValue() methods.