Array Basics Example

Oh arrays arrays. How some of us love to hate these. The question always comes up, heck even I ask it. Why use arrays now when there are so many other collection classes in the .NET framework. Especially when the other options are easier to work with and more functionality? You will see me from time to time reverting back to arrays because they teach me the basics of how the other collection options function under the hood. I mean look under the hood at the List collection class. If you look at the methods in that class and how they work. It’s basically doing array work behind the scenes.

One of the most confusing areas of programming, well it was for me when I started. Was the difference between value, reference types, stack, and heap allocation. Arrays helped me understand how reference types work. Meaning that when you make a change to an array anything else that references said array will change also.

So over the next batch of posts you will see various usages of arrays. The programs will be simple. I will still make the source code available if you want.

ArrayBasicsSolution ArrayBasicsProgram

 

Program.cs code contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayBasics
{
    class Program
    {
        static void Main(string[] args)
        {
            createDaysOfWeek();
            
            createArrays();

            Console.ReadLine();
        }

        private static void createDaysOfWeek()
        {
            string[] daysOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
            
            // Displaying the singular days of the week.
            Console.WriteLine("****Singular Days Of Week");
            foreach (string item in daysOfWeek)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            // Displaying the plural days of the week.
            Console.WriteLine("****Plural Days Of Week");
            makeDaysPlural(daysOfWeek);
            foreach (var item in daysOfWeek)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();
        }

        private static void makeDaysPlural(string[] daysOfWeek)
        {
            for (int i = 0; i < daysOfWeek.Length; i++)
            {
                daysOfWeek[i] = daysOfWeek[i] + "s";
            }
        }

        private static void createArrays()
        {
            // Creating some non-initialized arrays and variables.
            int x1;
            int[] x2;
            int x3 = 5;

            // Creating an empty array.
            int[] x4 = new int[5];

            // Creating an array with values.
            int[] x5 = new int[5] { 1, 2, 9, 16, 25 };

            // Another way to create an array an populate it with values.
            int eight = 8;
            int[] x6 = { 1, 2 * 2, eight + 1, int.Parse("16"), (int)Math.Sqrt(625) };

            // Different ways to enumerate the array values.
            Console.WriteLine("****ForEach Loop****");
            foreach (int item in x5)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            Console.WriteLine("****For Loop****");
            for (int i = 0; i < x6.Length; i++)
            {
                Console.WriteLine(x6[i]);
            }
            Console.WriteLine();
        }
    }
}

One of the most confusing areas of programming, well it was for me when I started. Was the difference between value types, reference types, stack, and heap allocation. Arrays helped me understand how reference types work. Meaning that when you make a change to an array anything else that references said array will change also. Once I figure out this concept things started to fall in place with other types.

For example look at the createDaysOfWeek() method call. An array of days of the week is created. Since arrays are reference types any changes made will reflect to this arrays memory allocation.

I’m making a call to the makeDaysPlural() private method and changing each day of the week from singular to plural. Yes I’m passing the array in as a parameter to the makeDaysPlural() method. But this parameter points to a reference. Meaning I don’t have to create another array, add my changes to it, and return it. I’m making a change directly to the objects in memory. This is demonstrated by the two foreach loops that are being run on the daysOfWeek array. One loop is before the makeDaysPlural() method is run and the next loop is run after. Both foreach loops are iterating thru the same array.

I’m also playing around with arrays in the createArrays() method. I’m showing different ways to declare arrays, initalize arrays, and populate arrays. Also showing the two main ways to iterate thru arrays with the for and foreach loops.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s