Properties
So properties are used like : arrayName.propertyName
The Rank property will tell you how many dimensions the array has.
The Length property will tell you how many elements are in a array.
Methods
The copy() method creates a copy of the array. The copy is shallow this means that it copies all the elements of the array whether its a reference type or a value type. However, it doesn’t create copies of objects it just copies the reference to that object. (a deep copy would create a copy of that object so the original and the copy would not be referencing the same object)
The Clear(Array array , int index, int length) method will turn the array starting from the index for length elements. So if you put 2 for index and 5 for length, it will set from index 2 to index 6 to the default for their type.
The GetLength(int dimension) method will return the length of the dimension you gave it. So say you are using a three dimensional array and you put a 2 for the parameter, it will give you the length of the third dimension. (it counts dimensions starting from 0 so for a single dimension array you would only be able to find the length of the 0 dimension without a error)
The GetType() method will return the type of a object it is, so if its a int it will return int if its a long it will return long, if the array is of a class you made it will return that classes name.( to clarify the full method is Object.GetType() not Array.GetType(), although a array is technically a object)
The GetValue(…) method has eight different version. Although there are really only 4 kinds, they just specify the indexes of 32 bit integer (Int32 or int) or 64 bit integer (Int64 or long). So I’ll be using int but each instance of the method could have long as the type for all the parameters.(Note: all parameters must be int or long you cant mix and match them within the same method). Also keep in mind that indexes start from zero in a array.
- So GetValue( int index1) is for use with single dimension arrays and will give you the value of the element at that index.
- GetValue( int index1 , int index2) is for two-dimensional arrays, and will give you the value of a array at index1 in the first dimension and index2 in the second dimension.
- GetValue( int index1, int index2, int index3) is for three dimensional arrays.
- GetValue(int[] indices) is for multi-dimensioanl arrays, the number of elements in indices should be equal to the number of dimensional the array you are trying to get a value from with element 0 being the index in the first dimension. (I am assuming)
The SetValue( Object value . . ) method basically the same as GetValue but you are setting the values at those indices, so you also need the value parameter which will be the first one.
The IndexOf( . . .) method has 6 different versions. I will be covering three because the other three just seem to be slightly different versions of these three. Also this only works for one dimensional arrays.
- IndexOf(Array array, object value) will return the first index that value appears in the array as a int. Everything is a object so don’t worry about the type.
- IndexOf (Array array, object value, int startIndex) will return the first index of the value starting from startIndex in the array as a int.
- IndexOf (Array array, object value, int startIndex, int count) will return the first index of the value from between startIndex and startIndex+count-1. So if start index is 2 and count is 5 it will be from between indices of 2 and 6.
The LastIndexOf( . . .) method is the same as IndexOf but its just the last index that the value appears at rather than the first.
The Sort( . . . ) method has 17 different version 9 look very similar to the other 8. Of which I will only be going over two because all the others involve a IComparer parameter which I believe is just something telling the method specifically how you want it sorted or it wants you to use another array as a key for the one your sorting.
- Sort (Array array, int index, int length) will rearrange the elements from index for length. So say index is 2 length is five the elements from 2 to 6 will be rearranged according to the default, which I believe would be something like alphabetical for strings.(so like if the array was the alphabet the first element would be “A”.
- Sort(Array array) will rearrange the entire array according to the default IComparer.
2 thoughts on “C# Arrays- useful methods(no examples)”
Comments are closed.