Home > Development > CSharp Arrays

CSharp Arrays


This follows on from my last post. This code was created as a snippet using the framework posted before and highlights pretty much most of the stuff you can do with arrays in .NET.

I am also going to start comparing Javascript arrays to .NET arrays as I am currently learning / educating myself with Javascript.

using System;
namespace CSharpInDepth
{
	public class ArrayExamples : IRunnable
	{
		public ArrayExamples()
		{
		}
		
		public void RunExamples ()
		{
			Console.WriteLine ("Examples of declaring and using arrays");
			
			int[] values1 = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
			
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine ("Number of dimensions: {0}", values1.Rank);
			Console.WriteLine ("Number of elements: ", values1.Length);
			
			Console.WriteLine ("Reversing array");
			Array.Reverse (values1);
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine ("Sorting the elements");
			Array.Sort (values1);
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine("Number 10 exists in the array is: {0}",Array.Exists<int>(values1, x => x == 10));
			int value = Array.Find<int>(values1, x => x.Equals(10));
			Console.WriteLine("The item found in the array matching the predicate is: {0}", value);
			
			Console.WriteLine("Finding the number 10 in the array");
			int index = Array.BinarySearch(values1,10);
			Console.WriteLine("Found at index position: {0}",index);
			
			int[,] values2 = new[,]{{1,2,3,4,5},{6,7,8,9,10}};
			Console.WriteLine("Values 2 rank is: {0}", values2.Rank);
			Console.WriteLine("Dimension 1: {0}", values2.GetLength(0));
			Console.WriteLine("Dimension 2: {0}", values2.GetLength(1));
			
			int[] values3 = new int[values1.Length - 1]; // Declare array reference for copying
			Array.Copy(values1,values3,values1.Length - 1);
			Console.WriteLine ("Printing new array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine("Clearing the array");
			Array.Clear(values1,0,values1.Length);
			Console.WriteLine("Count of the items in the array after clearing is: {0}", values1.Length);
			Console.WriteLine ("Printing cleared array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			
		}
	}
}

Blair….

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: