Contents

Kata 3

Setup instructions

  • Download the starting folder for this Kata.
  • Open a Command Prompt window and navigate to the starting folder using cd.
  • Open the starting folder for this Kata in VSCode.

Overview

In this Kata, you’ll look at how to write a computer program that accepts data from external sources - an ‘input’. You’ll learn how to process the input, then print the processed data to the Standard Output.

Kata Instructions

Open Program.cs. It should look like this:

using System;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

There’s quite a lot going on here. However we’re only interested in the line that looks like this:

static void Main(string[] args).

Below this line there is a curly brace {, followed by some C# code, followed by another curly brace }. The code inside the opening and closing curly braces is known as a ‘block’.

We’re going to start writing code inside the block labeled Main.

Delete the contents of Main so it looks like this:

static void Main(string[] args)
{
}

Outputs

We’re going to make our program output something to the Standard Output.

Inside Main, write another line below the one that looks like Console.WriteLine("Hello World!");, that looks like this:

Console.WriteLine("Goodbye Universe!");

Console.WriteLine enables the program to write to the Standard Output. In order to tell Console.WriteLine what to write, we need to provide an argument to it. In this case, we’ve provided the argument "Goodbye Universe!".

Notice the double quotes, ", around "Hello World!" and "Goodbye Universe!". We need these to tell C# that we want to write a word - known as a string. If we don’t use double quotes, C# tries to interpret the text as more instructions, which won’t work.

The whole thing should look like this:

using System;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Goodbye Universe!");
        }
    }
}

Remember to write a semi-colon (;) after each new line of code you write - this is very important!

Notice also that that our new instruction is indented inside the Main block. Indenting things correctly is very important in computer programming. To make sure your new instruction is indented underneath Console.WriteLine("Hello World!");, you can use the tab key on your keyboard.

Run the program using dotnet run and see what happens.

Arguments

Remember how the mkdir command works? Something like this:

mkdir MyFolder

MyFolder is considered to be the first argument for mkdir. We’re going to amend our program so that it can accept arguments in the same way that mkdir does.

Arguments provided to the program are stored under the name of args. This is short for ‘arguments’, and it contains all the arguments that the program has been given when it’s run.

First, delete all the code inside the Main block. Next, write the following inside the Main block:

Console.WriteLine(args[0]);

We’re now using args[0] as an argument to Console.WriteLine. The args[0] contains the first argument provided to the application. The line of code we’ve just written will therefore write its first argument to the Standard Output.

Run the program using dotnet run in the Command Prompt window. Provide an argument of your choice when you run your program. For instance:

dotnet run Cat

Will produce:

Cat

Arguments containing spaces need to be written inside double quotes. This is because spaces are used to separate arguments. For instance:

dotnet run Cat & Mouse

Will actually be considered to have 3 arguments - Cat, &, and Mouse. The correct way to do it would be:

dotent run "Cat & Mouse"

This will produce:

Cat & Mouse

The whole thing should look like this:

using System;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(args[0]);
        }
    }
}

Let’s be friends

Let’s make the program more friendly. Alter the code that we just wrote to output an argument so that it looks like this:

Console.WriteLine("Hello " + args[0]);

This will combine the word "Hello " with the value of args[0]. The + operator allows us to combine words like this. Notice that "Hello " contains a space character also.

This code will therefore write ‘Hello’, followed by the first argument, to the Standard Output.

Try it with your name:

dotnet run "Mikiel Agutu"
Hello Mikiel Agutu

The reason we need to put the ‘square bracket-zero’ [0] next to args is because args is an ‘array’. Arrays are beyond the scope of this Kata, and will be explained later. Just understand that args[0] gets the argument that the program was run with.

More inputs

Another way to input data to a C# program is using Standard Input. Let’s ask for the user’s age using the Standard Input.

We need to prompt the user to input their age. Inside the Main block, below the code we’ve already written, write:

Console.WriteLine("Please tell me how old you are!");

This code will print Please tell me how old you are! to the Standard Output.

To get the user’s response, below this line write:

string age = Console.ReadLine();

This uses Console.ReadLine() to read from the Standard Input.

The string age = part will store the user’s age in a ‘variable’ called age. Variables are beyond the scope of this Kata, and will be explained later. Just understand that the user’s age from the Standard Input is stored under the name age.

Below that, write:

Console.WriteLine(args[0] + ", you are " + age + " years old!");

We’ve used the + operator to combine words and variables together.

Your program should then look something like this:

using System;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello " + args[0]);
            Console.WriteLine("Please tell me how old you are!");
            string age = Console.ReadLine();
            Console.WriteLine(args[0] + ", you are " + age + " years old!");
        }
    }
}

Try dotnet run with your name, like:

dotnet run "Mikiel Agutu"

You should get an output like:

Hello Mikiel Agutu
Please tell me how old you are!

You’ll then get a blank line in the Command Prompt window. This means the computer is waiting for you to input something. It’s your prompt to write a number for your age.

In my case I write:

22

The program then replies with:

Mikiel Agutu, you are 22 years old!

Finished!

You’ve seen how to get input and produce output from a C# application. You’ve used arguments and Standard Streams to do this.

Try on your own…

Q1

It’s possible to configure the Standard Input of the program we’ve just written without making any changes to the code.

This is possible with the ‘pipe’ operator in the Command Prompt. The Pipe operator looks like this: |.

Piping allows you to set the output of one command to be the input of another. If I ran:

echo 22 | dotnet run "John Smith"

echo 22 writes 22 to the Standard Output. The pipe operator means that echos output is set to be the Standard Input of the next command - in this case our program.

It means that Console.ReadLine will read from the output of echo 22, instead of from the Command Prompt. Try running this and see what happens.

Q2

Similarly, it’s possible to configure the Standard Output stream too. You can do this by setting Standard Output to be a file instead of the Command Prompt window with the redirect operator that we saw in Kata 1.

Run the program using echo and dotnet run, but use the redirect operator to set the Standard Output to be a file. It might look like this:

echo 22 | dotnet run "Mikiel Agutu" > file.txt

Inspect the contents of file.txt using more, the VSCode Explorer window, or the File Explorer.

Copyright Mikiel Agutu 2019