C# Arrays- useful methods(no examples)

C# Arrays- useful methods(no examples)

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.

  1. So GetValue( int index1) is for use with single dimension arrays and will give you the value of the element at that index.
  2. 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.
  3. GetValue( int index1, int index2, int index3) is for three dimensional arrays.
  4. 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.

  1. 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.
  2. IndexOf (Array array, object value, int startIndex) will return the first index of the value starting from startIndex in the array as a int.
  3. 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.

  1. 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”.
  2. Sort(Array array) will rearrange the entire array according to the default IComparer.
C# Jagged Arrays

C# Jagged Arrays

So, a Jagged array is basically a array of arrays. This means that the elements in a jagged array are single-dimensional or multi-dimensional arrays. You initiate a jagged array by doing:

type[ ][ ] nameOfArray = new int[#][ ];

-or in the case of the elements being multidemnsional arrays-

type[ ][ , ] nameOfArray = new int[#][ , ];

The number is optional(# can be a symbol for number). It’s just telling the compiler how many elements are in the jagged array, and c# doesn’t require you to tell it that when you initialize the array.

You put elements into the array by:

nameOfArray[indexNumberOfElement] = new type[#];

-or multi dimensional-

nameOfArray[indexNumberOfElement] = new type[#,#];

-or with the arrays being specified-

nameOfArray[indexNumberOfElement] = new type[#] { Var1, Var2,etc};

You can also declare and initialize at the same time:

type[ ][ , ] nameOfArray = new int[#][ , ]

{

new type[#,#] { {Var1,Var2,etc} ,{Var3,Var4,etc}, etc},

new type[#,#]{ {Var1,Var2,etc} ,{Var3,Var4,etc}, etc},

etc

}

For the jagged arrays the number of elements in the array elements can vary, as in a array of 4 elements and a array of 20 elements can be in the same jagged array. You can also leave out the “new int[#][ , ]” and it will still compile.

Microsoft doc link

C# arrays – basic information

C# arrays – basic information

An array is a place you store multiple variables of the same datatype. There are three types of arrays single-dimensional, multi-dimensional and jagged.

The single and multi arrays are what they sound like with the single dimensional array acting like a row, while the multi is like a grid, or a cube. So the single array is made like so:

type[ ] arrayName = new type[numberOfValuesBeingPutInArray];

or

type[] arrayName = {value1, Value2, Value3,etc};

You can also make a array by by doing:

type[] arrayName = newType[] {{value1, Value2, Value3,etc};

The multi-dimensional array is very similiar as it essentially just changes the [] to [ , ] or however many commas like so:

type[ , ] arrayName = new type[ numberOfRows, numberOfColumns ];

type[ , , ] arrayName = new type[ numberDimension1, numberDimension2, numberDimension3 ];

type[ , ] arrayName = {{V1,V2,etc}, {V1,V2, etc} , etc};

type[ , ] arrayName = {{{V1,V2,etc}, {V1,V2, etc} , etc}}, {{V1,V2,etc}, {V1,V2, etc} , etc} , etc} ;

You can essentially have as many dimensions as you want so long as you have the memory space for it and your compiler can handle it. If you declare an array without initializing it(assigning it values either with new or the {}, you would need to use the new operator to assign the values similar to what’s shown in the third example for single arrays.

You cannot change the length or number of dimensions after the instance of the array is made. If you don’t declare what the values in the array are they are the default for the type( 0 or null). The elements can be of any type so long as they are within the type of the array. This means that if you make the type Object (as in the object class that all types should be a part of) you can put whatever value want in it, but if you make an int array you can only put ints not anything else. When calling from an array the first value is at zero and the last value is at the number of values-1. So, arrayName[0,0] would call the value in the first column and the first row.

For example:

int array[] = {1,2,3,4,5};

System.Console.WriteLine(array[0]);

// the console will print out 1

System.Console.WriteLine(array[4]);

// the console will output 5

You can use a foreach loop to go through an array, which will be discussed in a different post. Along with some common and useful methods used with arrays.

A jagged array is an array of arrays , and this is the link to the more detailed post on them.

The Microsoft tutorial for this is here.