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 to work with our C# project in our code editor, Visual Studio Code. If you missed it, that part of the series is available here.
In this part of the series, we’ll explore the structure of Program.cs to understand how the application works. Once we understand how the application works, we can modify it to be an interactive application.
Exploring the Application
Open Program.cs in Visual Studio Code.
Using Directive and Namespaces
On Line 1, we see a using directive for the System namespace.
A namespace is a way to organize classes.
.NET has thousands of classes available to use in your application but you will rarely if ever, use all of those classes in a single file. To make .NET (and your own) classes more manageable, we use namespaces to organize and control access to those classes.
A using directive tells the C# compiler that we are using classes from a particular namespace.
In this case, we are telling the C# compiler that we are using classes from the System namespace. In Program.cs, the class we are using from the System namespace is called Console, which we will talk about a bit later on.
In the documentation for the Console class, we can see that it belongs to the System namespace:
On Line 3, you can see that are defining our own namespace named src.
The declaration of a new namespace is followed by opening and closing curly braces on Line 4 and Line 12 respectively. These curly braces are used to delimit the namespace — everything inside of these curly braces is part of the namespace.
On Line 5, we have a class definition for a class named Program. Once again, the Program class is delimited by curly braces.
A class is a way to encapsulate and organize code used in Object-Oriented Programming. We’ll talk about classes in much more detail when we talk about Object-Oriented Programming.
On Line 7, we have the definition for a method called Main.
A method is a code block that contains statements. We can see that the method Main contains a call to another method Console.WriteLine() which we will talk about shortly.
A method is written once and can be called many times in a program, so it is a convenient way to re-use code.
The structure of a method definition is:
<keywords> <return type> <name>(<parameters>)
The Main method is declared with the keyword ‘static’. This is one of many keywords that can be used when declaring a method.
We’ll talk about keywords in detail when we dive into Object-Oriented Programming.
As well as executing code, methods can return a value after executing. The type of the returned value is included in the method definition.
A method defined as having the return type ‘void’ does not return a value.
You can pass data to a method using method parameters. The values passed to a method can be used inside of the method to influence the return value.
For example, you may define a method that adds two numbers together. The parameters for the method are two numbers, a and b. The return value of the method is the sum a + b. Each time you call the method in your program, you can pass different numbers to the method to get a different return value.
The Main method has a parameter called args. We’ll talk about args more in the next section.
Sometimes, there is confusion about the terms ‘parameters’ and ‘arguments’. When you define a method, you also define the names and the types of the values that are passed to the method. These are the method’s parameters.
When you call the method in your program, you pass an actual value, called an argument, to the method for each parameter.
Finally, on Line 9 we have a call to the method WriteLine which is part of the Console class.
The method WriteLine accepts a parameter of the data type String. In our application, we pass the argument “Hello Ken!” to the method.
WriteLine takes this argument and prints it to the Command Prompt window.
In this part of the series, we explored that structure of the Program.cs file. Along the way, we got a brief introduction to concepts like directives and namespaces, classes, and methods.
Don’t worry if those concepts are unclear to you — we’ll go into much more detail as the series progresses. As you work through examples, you will begin to understand each concept better.
In the next part of the series, we’ll learn how to make our application interactive by accepting user input and using that input to modify the output of our application. 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.