Sunday, October 20, 2019 13:40

Structure of a C# Console program

In our previous post, we created our first C# program, a console application that displayed a single sentence and which contained the following instructions:

It is now time to analyze the structure of a C# Console program. These instructions are very similar to the ones you will encounter in most of the programs you will ever write. As you can see, our program starts with a few using instructions. The using instruction asks the compiler to use the containing of a file. In the case of our program, the using instruction asks the compiler to include the contents of some files, contents which are named System.Collections.Generic, System.Textetc. These contents are stored in what .NET programmers refer to as namespaces. You don’t have to worry about what a namespace is for the moment, all you need to know is that they contain additional codes that we can use in our programs. Whenever you use a using directive, you don’t have to manually type those codes, and you also don’t have to re-invent the wheel. For instance, if you would have to write a program that calculates square roots, you don’t have to create a function for doing that. Instead, you would insert using System.Math; which contains a function for calculating square roots, and other math functions.

After the using statements, you see two concepts: namespace and  classFor now, think of a class as being an object. A real world object, like a car, a house, a cat, etc. Classes are objects, and they contain properties and other stuff. Namespaces, on the other hand, are a collection of classes, grouped under the same criteria. For instance, we could have a namespace called Universe, which would contain some objects (classes): stars, planets, galaxies, etc. Now it makes sense to group those objects in a namespace, because they have something in common: they are PART of the Universe.

Next line in our program is

Every console application that you will create will contain a line similar to the void Main instruction. In the future, you will notice that it will be far easier to group the instructions that a program has to execute so that it will be easier for you and other programmers to understand what the program does. What you have to remember is that in a Console application, the void Main instruction is executed first. What about the { } characters? For now, all you have to know that C# computer programs have their instructions grouped in blocks, and each block starts with { and ends with }This is how the compiler can understand where a block begins and ends. Remember that for any open bracket, you must use a closing bracket.

Finally, in our program, INSIDE the block of our Main method, we have two lines of code:

By using Console.WriteLine we instructed our program to display a text in its window. By using Console.Readwe instructed it to not close the window after the execution ends, but to wait for input (in this case, keyboard text, followed by Enter key) from us.

Comments

comments

Tags: , ,

Leave a Reply