Let’s learn about strings , arrays and the .net console!

In our continuing series on learning the C# language, today we’re going to focus on completing a project that illustrates some of the most basic parts of programming. While we are focusing on the C# language the concepts that we will focus on here are applicable to all programing languages.

As always I have included a link to the project we will be discussing.  This is the fully completed project and I only am including it for reference in case I do a bad job of explaining something or you need it for troubleshooting. I suggest that you complete the whole project by hand.

We will be exploring some of the very basic concepts of C# and Visual Studio 2019.I will not be taking much time to explain the concepts here, as I have a supplemental post that covers most of  them in more detail.

Concepts Covered:

      • Variables – Variables allow you to name a value that is likely to change in your program for use throughout run-time.
      • Collections – Collections are groups of variables or objects that let you hold multiple values for use in your project.
      • The .net console – The Console is a .net construct that lets you read and write text for display to the user. (This will be the main part of our program)
      • Basic loops / control flow – Loops let you perform an action (usually in conjunction with variables to complete actions in a program.)

OK! Now let’s get down to talking about the code in this app!

This is a very basic app that takes the age old concept of creating a “Hello World” program and modifying it to say hello to a range of names which we have added to a collection (also called an array) .

Create a new project

Choose .net console app

Name it “Hello” or whatever you like.

The editor will open and now you will be looking at Program.cs in the editing window for this tutorial we will do all of the code in the Main() method to keep this simple.

Here are the main parts of this app!

      1. Create a string array and add some names to it.

        string[] names = new string[4] { "Chad", "Wolfie", "Barnyard", "Bertie" };

        Remember when we talked about variables? Well an array ( or collection ) is kinda like a box full of variables. You can think of an array like an excel spreadsheet with only one column (in most cases). After you initialize it you can access it by using the name you gave it some square brackets and the index of the element that you want. (as you will see in the code example)
        One thing I must stress here that can be confusing to new programmers.. When we instantiated this array we told it to make an array that can hole 4 elements. (new string[4])  but when we access the elements the index always starts at 0 so even though you specified 4 items the max index you can access is [3]

    index element
    0 Richie
    1 Joanie
    2 Chachi
    3 Potsi
    4 Fonzarelli

2. Using a for loop we will move through the array that we just created. 

for (int i = 0; i < names.Length; i++)

{

  //write out the console hello + each name

  Console.WriteLine("Hello {0}", names[i]);

}

 

There is alot going on in the code for a for loop so let’s work our way through it and see if we can make some sense of it.

                     First of we have the keyword (for) and then we have the conditions for the loop.

        • int i = 0;
          Instantiate a temporary variable (named i)  that we can use as both a counter in the loop but also as a way to break out when it is time.
        • i < names.Length;
          We have a couple things happening here. We are telling the loop that we want to continue running the loop as long as i is less than names.Length.
             When we refer to names.Length we are referencing a built in function of the array object that lets us get the Length of it.
        • finally we have i++;
          This just tells the program to add one to i each time the command is run. (each time we loop)

Next we have the code that is being performed each time we loop. This piece is pretty easy to understand thankfully. We are going to have the console Write a message on each loop.
  Console.WriteLine("Hello {0}", names[i]);

The Console.WriteLine() method takes in a string and simply writes it on a single line in the console.

As far as the string that we see here goes:

        • Everything between the ” ” is part of the output message except the {0}.
        • This is called substitution , we are telling the program that it should substitute the variables that we are supplying after the comma.
        • In this case the variable is the element stored at the index that we are providing between the square brackets.
        • The handy thing to note here is that we have the “i” in the square backets, because we are looping and i is getting incremented on each loop this provides us with a nice clean way to work our way through a collection.

 

That’s it! You did it! I have included the completed code below! On the next tutorial we are going to take this same program and modify it to ask the user to input names instead of hard coding them! I look forward to seeing you there!

 

Completed program:

namespace TheDevelopingDeveloper.net
{
 class Program
 {
  static void Main(string[] args)
  {
    //create a string array
    //add some names to the array
    string[] names = new string[4] { "Chad", "Wolfie", "Barnyard", "Bertie" };
    //use a for loop to iterate through the array
    for (int i = 0; i < names.Length; i++)
    {
     //write out the console hello + each name
     Console.WriteLine("Hello {0}", names[i]);
    }
   }
  }
 }

 

 

Leave a Reply

Your email address will not be published.