User FAQs

If you have a question, please send it to us.


1) Is an Array a value data type or a reference data type ?
All arrays are reference data types.
They are implicitly derived from System.Array which is derived from System.Object.

string[] myArray = new string["Mon","Tue","Wed"]; 
MessageBox.Show(myArray.GetType().FullName);
// displays 'System.String[]'

2) What is the difference between a Collection and an Array ?
Collections are not strongly typed and can store different data types which are dynamic in size.
Arrays are strongly typed collections of the same data type that have a fixed length that cannot change at run-time.


3) How would you return the size of a 1 dimensional array ?
This array is a zero based array.

var myArray = new[] {1, 2, 3};               // implicitly typed, inferred from items 

int arraySize = myArray.Length - 1; // the total items in the array
int arraySize = myArray.GetLength(0); // the size of a particular dimension
int arraySize = myArray.GetUpperBound(0); // the highest index in the array

4) How would you return the size of a 2 dimensional array ?
This array is a zero based array.

int[] myArray = new int[ , ] { {1, 2}, {3, 4}, {5, 6} } 

int arraySize = myArray.Length - 1; // the total items in the array
int arraySize = myArray.GetLength(0);
int arraySize = myArray.GetUpperBound(0); // the highest index in the array

5) How would you determine the number of dimensions in an array ?
You can use the Rank property.

int dimensionSize = myArray.Rank; 

6) Can you create arrays that are not zero-based ?
C# does support non-zero based arrays although using them is not recommended.
You can use the CreateInstance method to create non-zero based arrays.

// 1-dimensional array, zero based - 10 items, starting at 0 
System.Array myArray2 = System.Array.CreateInstance(typeof(int), 10);

// 1-dimensional array - non zero based - 10 items, starting at 5
// the actual data type is int[*]
// this items in this type of array are not displayed in the Locals window
System.Array myArray3 = System.Array.CreateInstance(typeof(int), new int[] { 10 }, new int[] { 5 });
myArray3.SetValue(5, 5);
System.Diagnostics.Debug.Print(myArray3.GetValue(5).ToString());

// 2-dimensional array, zero based - 10 items starting at 0, 0
System.Array myArray4 = System.Array.CreateInstance(typeof(int), 5, 2 );

// 2-dimensional array, non zero based - 10 items, starting at 5, 1
System.Array myArray5 = System.Array.CreateInstance(typeof(int), new int[] { 5, 2 }, new int[] { 5, 1 });

7) How would you return the size of a 2 dimensional array that is not zero based ?
This array is not a zero based array.

int[] myArray = new int[ , ] { {1, 2}, {3, 4}, {5, 6} } 
int arraySize = myArray.GetLength(0);
int arraySize = myArray.GetUpperBound(0); // the highest index in the array

8) What is the fastest way to add up all the numbers in an array ?
The fastest way to add up all the numbers is to use LINQ.

int myArray = new int[] { 1, 1, 1, 1, 1, 1, 1 } 
int myTotal = myArray.Sum();
// pre .NET 3.5 - using an anonymous delegate
Array.ForEach(MyArray, delegate(int i) { myTotal += i; } );

9) What is the difference between a Rectangular Array and a Jagged Array ?
A rectangular array has the same dimensions and sizes.

string[, , ,] myArray; 

A jagged array can have different dimensions and sizes. Also called an array of arrays.

string[][][] myArray; 

10) What is the difference between a deep copy and a shallow copy ?
Deep Copy - copies across both Value data types and Reference data types as entirely new objects.
Reference data types are copied across in their entirety as new objects.
This creates different but identical objects.
Shallow Copy - copies across both Value data types and Reference data types.
Reference data types are copied across but will be pointing to the same objects.
This creates a copy pointing at the same objects.


11) What is the difference between System.Array.CopyTo and System.Array.Clone
Both perform a shallow copy.

System.Array.CopyTo   // copies items into an existing array. This is more flexible 
System.Array.Clone // returns a new array with all the same items.


© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext