Understanding the Application

C# From Scratch Part 2.4

Ken Bourke
Dec 22, 2020 · 5 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 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.

Classes

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.

Methods

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>)

{

<statements>

}

Keywords

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.

Return Type

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.

Parameters

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.

WriteLine

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.

What’s Next?

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.

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