Making It Interactive

C# From Scratch Part 2.5

Ken Bourke
Dec 28, 2020 · 6 min read

Welcome to another part of the series C# From Scratch, a course dedicated to teaching you everything you need to know to be productive with the C# programming language.

In the previous part of the series, we learned how our program is structured. While exploring the structure of the program, we learned about some important concepts from the C# language including using directives and namespaces, classes, and methods. If you missed it, that part of the series is available here.

In this part of the series, we’ll make the application interactive by accepting input from a user and using that input to modify the output.

The Main Method

In this part of the series, we’ll focus on the Main method which is where most of the code for our application currently lives. We mentioned previously, that all of the code in the curly braces after a method definition is executed when the method is invoked. So how does the Main method get invoked?

It turns out, that when the .NET Runtime launches your application, it starts execution at a pre-defined entry point. That entry point is the Main method. When you use the command dotnet run to run your application, .NET looks for the method named Main invokes that method.

Currently, the code inside of that method tells .NET to print the string “Hello Ken!” to the Console window.

We want to change this code so that we can print a customized string to the Console window. The contents of the string is based on input received by the user when they run the application.

Parameterization

In a method definition, we can define the parameters that the method takes. Parameters are pieces of data that are passed to the method when it is invoked, they are inputs that the method can use.

A method can have zero or more parameters as part of its definition. Parameters are defined in the parentheses after the method’s name. Every parameter has a data type and name.

The Main method has one parameter called args, which is short for arguments. These are the arguments that are being passed to the application by a user when the application is run from the Command Prompt.

The data type of args is an array of strings. We can see that args is an array of strings because there are square brackets after the string keyword.

Arrays and Strings

In C#, a string is a sequence of characters between double-quotes. In this example, “Hello Ken!” is a string.

An array is a collection of pieces of data of a certain data type. A string array is a collection of strings.

You can access a specific piece of data in an array using an index. Let’s imagine that args contains the strings “John”, “Joe”, and “Jean”. To access each string, you can provide the index of the string you want to access after the array name.

In the picture below, you can see how you can access each string in the args array using the index.

It is important to know that the first element of an array lives at index 0. This is because C# uses zero-based indexing.

Processing Inputs

In our application, we expect that the user will pass one argument to the application. This argument is the name of the person that the user wants to greet.

When the user passes one argument to the application, the args array will contain one element. We can access this element in our application using the notation args[0].

We want to use args[0] to make the output of the application. To do that, we have to build a string with a hardcoded part (Hello ) and a variable part (args[0]).

How can we build a string in C#?

String Concatenation

String concatenation is the process of joining (or concatenating) several substrings together to create a string.

In C#, + is the concatenation operator.

In our application, we could concatenate the string “Hello ”, the string args[0], and the string “!” to produce a customized greeting. Here’s how that looks in our program:

String Interpolation

Another way to build a string in C# is with string interpolation. Although both methods of building strings are valid, string interpolation can be a bit easier to read.

With string interpolation, you indicate to the C# compiler that the string might contain interpolation expressions using a dollar sign at the beginning of the string. These interpolation expressions are injected into the string with curly braces and resolve to a string.

This sounds confusing, but looks quite neat in the code:

Neither method of building strings is wrong and neither method is more correct. Like many things in programming, you can solve the problem in multiple ways and the way that you choose will be down to your own personal preferences.

Running the Program

Save the changes that you have made to Program.cs and in the terminal window of Visual Studio Code, navigate to your src folder using the cd command.

Now you can run the program using the dotnet run command and pass arguments to the application.

When your application runs, you should see the output change based on the arguments that you are passing to the application.

Out First Bug

What happens when you run the application without passing in any arguments?

The application crashes and the output indicates that there is an unhandled exception. Something is going wrong with out application — with the changes that we’ve made, we have introduced a bug.

What’s Next?

In this part of the series, we learned how to accept input from a user and use it to modify the output of the application. Along the way, we learned about string concatenation and interpolation.

We also introduced a bug to our application — it crashes when a user runs the application without passing in a command line argument.

In the next part of the series, we’ll learn how to use Visual Studio code to debug our application and investigate the root cause of the bug we have discovered. You can find that part of the series here.

Missed a part of the series? You can find an overview of the series and links to all previous parts on the index page.

The Startup

Get smarter at building your thing. Join The Startup’s +724K followers.

Ken Bourke

Written by

Automation Engineer, Technology Enthusiast, Freelance/Consulting. Official website: https://www.kb-controls.io/

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Ken Bourke

Written by

Automation Engineer, Technology Enthusiast, Freelance/Consulting. Official website: https://www.kb-controls.io/

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +724K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store