C# Data Structures – Array

The Array is one of the simplest and most memory efficient of the data structures.  An array is a fixed list of items.  These items can be of any type and 99.9% of the time are the same type of items.  Array items can be accessed directly without the need to iterate through.  Arrays like most other data structures is zero based. Here is an example of a string array:


var array1 = new[] {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};

For an example lets say we know without a shadow of a doubt that the item want is in the zero location we can do the following:

Console.WriteLine("This element is in the {0} position of the array: {1}", 0, array1[0]);

Console write line will print out the word ‘the’.

As we initial stated Arrays are fixed length.  What if we have two arrays that need to be merged.

var array1 = new[] {"The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
var array2 = new[] {"added", "array" };

// Create a new array wtih the combined length
var array3 = new string[array1.Length + array2.Length];

// copy all the items in array1 starting at the zero item into array array 3
Array.Copy(array1, 0, array3, 0, array1.Length);

// copy all the items in array2 starting at the zero item into array 3 but start at the last index from array1
Array.Copy(array2, 0, array3, array1.Length, array2.Length);

this will return a new array of the combined items.

This entry was posted in C# and tagged , . Bookmark the permalink.

One Response to C# Data Structures – Array

  1. news feed says:

    Thanks for the strategies you have contributed here. Moreover, I believe there are some factors that will keep your automobile insurance premium all the way down. One is, to bear in mind buying autos that are inside the good report on car insurance companies. Cars which can be expensive tend to be more at risk of being robbed. Aside from that insurance coverage is also in line with the value of your automobile, so the more pricey it is, then the higher the premium you pay.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s