Introduction

In this article, we’ll examine the anatomy of a C Sharp program in a bit of detail, and relate in some of the concepts we’ve discussed prior to this.

A Basic Application

If you recall the prior application we wrote, our “Hello, world” application, we had a bunch of lines of code that were used to make something relatively simple happen. We will use this simply application to understand what elements are essential for an application. The answer is, not much.

Open up Visual Studio and when you are prompted to open an existing project or start a new one, click on the solution for the prior application we created (the one called HelloWorld). You should see our code from last time open again.

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

namespace HelloWorld
{
	class Program
	{
		static void Main(string args[])
		{
			Console.WriteLine("Hello, world!");
			Console.ReadKey();
		}
	}
}

This example, which I covered in the article where we set up Visual Studio, is a simple application which opens up a console (command line) window, prints “Hello, world!” and then waits for you to press any key on your keyboard. When you do, the program ends. Simple, right? It’s not fancy, but it demonstrates that applications need to be complex.

In the case of this application, we’re not really intending for anything critical to happen. So let’s look at the actual breakdown of what’s happening, and why.

The first 5 lines of the application all start with “using”. and have some text after it. The first line in Visual Studio will be bright, whereas the other 4 will be more of a gray color. These lines are declarations of libraries that will be used in this particular file of the application. Or rather, these are the 5 default libraries that Visual Studio THINKS we’re likely to need for our application. The 1st line is the only actual one that is used. That’s why it’s shown with lighter text.

Initially when we created the application, the file did not include the lines that start with “Console”. At that stage, all of the “using” statements were dimmed to indicate that none of them was in use. If we remove the 2 – 5 lines and press the play button at the top of our Visual Studio window, the application will run exactly the same as it did the last time, without any errors. So does that mean we didn’t need those lines? Yes… in this case. If you stop the program, try removing the “using System;” line and try running the application again. You’ll get a message that says that the build failed, and will ask if you’d like to run the last successful build. You can answer no and let’s look at the code again.

You should now see a red, rippled underline on both of the Console lines. That red underline indicates that there is an issue with the code. If you hover over it, you’ll see a warning that “The name ‘Console’ does not exist in the current context”.

If you click on the link that says “Show potential fixes”, you’ll get a further menu.

This gives you options for how you can fix your code. The first option is “using System;” and clicking it will re-add the line to the top of the file for the System library.

Now, if you hit the play button again, the application will build and run.

Libraries

You really don’t want to have to create all of the necessary underlying code necessary to print text on a screen. It’s actually a lot of work to build a fundamental base set of functionality like that; it’s a lot of effort, and frankly, there are already sets of code that do the work for you. That’s what a library is. It’s a collection of logic and instructions for how to perform one or more operations, conveniently packaged up for your use.

In simpler terms; all the stuff you need to regularly do are available to use without you needing to manually create it for each project. The System library contains many of the basic functions that are used throughout your application, especially in terms of handling input from a user and displaying output back to them.

Other libraries exist which handle functions of a different nature. When we need those functions, we can add them simply by adding the appropriate library with a using declaration.

The significance of the semi-colon

You may have notices, looking at the code in the demonstration program, that numerous lines ended with a semi-colon. The semi-color character ( ; ) is used as an end-of-instruction indicator. This is actually a common trait in many languages that follow the syntax of the programming language of C – C Sharp is one such language.

The semi-colon is not used on every line. You’ll notice that it’s not at the end of the line that states “namespace HelloWorld” or “static void Main(String args[])”. They are found at the end of all of the using lines, and at the end of the Console lines. These are the lines that actually indicate a step to be taken. Other lines like the namespace or static void main are indicators of a point within a program, or a collection of instructions, but do not actually act as direct instructions themselves.

You’ll come to understand that in time, when you are explicitly indicating a function the computer is to perform at that step, THAT is when you shall need a semi-colon.

Curly Braces

Much like the semi-colon, the curly braces { and } are a special character used for structuring an application. The curly braces are, in fact, a matched set. They indicate a block of code. If you look carefully, you will notice that after we have the namespace HelloWorld text, the next line down is a left-hand or opening curly brace. At the very end of the program is a right-hand or closing curly brace. This is one of the simplest points about organizing your code. These curly braces indicate that everything contained within them is a part of whatever it is that precedes the opening brace.

In other words, the void static main and everything else within the first set of curly braces are a part of the namespace. Additionally, everything within the curly brace set that follows void static main is a part of void static main.

This is known as nesting or blocking – we are indicating that the contents are nested as part of a structure, and that block is nested in a larger structure. The finer details of this we will cover over time, but for now, the most important thing to note is that whatever you open, you must close, and in the appropriate order. The appropriate order is something we’ll cover over time.

One More Pass

So we’re going to look at HelloWorld one more time, and dissect this code with a bit better an understanding of what some of this is. So going back to our full code:

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

namespace HelloWorld
{
	class Program
	{
		static void Main(string args[])
		{
			Console.WriteLine("Hello, world!");
			Console.ReadKey();
		}
	}
}

Line 1 – indicate that we need the System library

Line 2 – indicate that we need the System.Collection.Generic library

Line 3 – indicate that we need the System.Linq library

Line 4 – indicate that we need the System.Text library

Line 5 – indicate that we need the System.Threading.Tasks library

Reminder: we included the libraries on lines 2 through 5, but we didn’t ACTUALLY make use of them, so they will show in a dimmer coloring in Visual Studio to indicate that they are not in use. We will discuss at a later time when it’s appropriate to remove those statements.

Line 6 – a blank line. This is not a requirements, but it makes it far easier to organize your code if you segment it some. And extra line returns don’t end up meaning anything in this language, so having some extra space might just make it easier to read.

Line 7 – Indicates that all of the code within the next paid of opening and closing curly braces is part of the namespace of this application. We’ll talk about namespaces more at a later time, and once we do in the proper context, you’ll see a tremendous value in them as an organization and containing element.

Line 8 – the opening curly brace for the HelloWorld Namespace.

Line 9 – indicates that this is a class; a sort of advanced data structure object which we will cover in the near future. The class is of type “Program”. Again, this will be important in the near future. For now, just be aware that the line of code is there.

Line 10 – the curly braces indicating all the following content is part of our “Program” class.

Line 11 – a static method named ‘Main’ that returns nothing (void). Inside of the parenthesis are the words string args followed by a pair of square brackets [ and ]. This is an indicator that our ‘Main’ function (actually method) takes in a parameter that is a string – if you recall from the last article, a string is a group of characters. The [ and ] indicate that the value being passed in is something called an array. We’ll talk about arrays more soon as well. As this is a void function, when it is completed, no data comes back from it. There are other options as well when it comes to this.

Line 12 – a curly brace indicating that the following content is part of the “Main” method;

Line 13 – a call to the Console object that is part of our System library, telling it to write a line of Text to the output window. The output is the literal text “Hello, world!”. This method also automatically causes a carriage return (new line) at the end printing the output. There is another version of this method called “Write” which works very similar, but does NOT include a new line at the end. If you were to have another Console.Write command following it, the text would begin immediately after the end of the first block of text, with no space, and no new lines.

Line 14 – this line is a call to the Console object to wait for the first key to be pressed on the keyboard. Once a key is pressed, the program will continue on to the next instructions. This is a simple way to have a “press any key to continue” hold in your program. And you can make it fun for yourself by waiting to see if a friend searches for the “any” key.

Line 15 – end of the structure that belongs to line 11; closing to the open brace on line 12.

Line 16 – end of the structure that belongs to line 9; closing to the open brace on line 10.

Line 17 – end of the structure that belongs to line 7; closing to the open brace on line 8.

Conclusion

Believe it or not, we actually now have enough fundamentals down to start working on a program in more detail. The next lesson of the introduction will be to modify the HelloWorld program to take a bit of user input and then to return output as a result. After that, you’ll be ready to start working on more intricate and advanced topic. Check back soon for part 4 of the introductory articles.