Note: started on January 29th 2021.
# **Conquering Visual C#**
# Section 1 - Introduction
## Lesson 1.1 - What is programming and why should one learn it?
**What is programming?**
Before we start addressing this question, we’d like to demystify a huge myth about programming: Programming is not sitting in a cave all day wearing a Guy Fawkes mask (commonly known as hacker mask or anonymous mask), typing 0’s and 1’s in a terminal, and drinking fizzy drinks; just like programmers aren’t cavemen who spend all their lives doing nothing else other than spending hours and hours sitting in front of a computer, writing code.
Just like painting, programming is a form of art that requires time and practice to master. Just like artists, programmers use their creativity combined with their skills to produce some sort of art.
To summarize, computers don’t think like humans: they don’t have the capacity to think “outside of the box” like humans do, that is to say, computers have strict sets of rules that they must follow, in order to work. Computers are beings deprived of imagination and creativity.
That’s where programming comes in handy: A programmer (the artist) uses their creativity and skills in order to produce art (software). You can think of programming as a way of giving a computer instructions, as a way of teaching the computer something, ranging from simple programs such as one that deletes a file to huge Neural Networks.
**Why should I learn programming?**
Normally, people would say that programming is worth learning because of how much money you can make out of it, but that’s not really a valid reason to learn programming, from our point of view.
Imagine yourself sitting on a chair in front of a computer doing something you don’t even like for a wage. Regardless of how much your employer is willing to pay you, in the end it will just be an annoyance to you, one more thing that’ll make you feel even more stressed.
The answer to this question (from our point of view) is that you should learn programming for the same reason why you probably learnt how to ride a bike during your childhood: Because you felt interested about it; because you felt like it would be fun to learn how to ride a bike.
With programming, it’s exactly the same thing: You should learn it if you really are interested in computers and how they work.
## Lesson 1.2 - How does it work?
Imagine yourself as a tourist in a foreign country and the computer as a native. You try to talk to them, but they don’t understand you. You try to use sign language, but they still don’t understand. Weird, right?
What we mean by this is that a computer cannot directly understand your code: the only language a computer understands is machine code (commonly known as binary) therefore needing an intermediary to translate your code into machine code.
There are two mutually exclusive types of intermediaries, that work in distinct ways: compilers and interpreters.
**Compiler**
In a compiled language, such as C and C++, programs must be compiled before being executed, that is to say, the program must be translated into machine code that the computer can understand.
The compilation process works in the following way: The compiler examines your code, translating it into machine code, wrapping that translation in a file that can be directly executed by the programmer.
This makes the program run faster (as the computer can directly understand the language) and the startup time bigger (because the code has to be examined and compiled before being executed).
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116184/compilation_usvrih.png"></img>
*Flowchart demonstrating how a compiler works.*
</center>
**Interpreter**
Instead of translating all the code into machine code at once, an interpreter translates the line at the same time it is being executed thus making the program slower and the startup time smaller (as no examination is performed).
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116178/unnamed_wiwpj1.png"></img>
*Flowchart demonstrating how an interpreter works.*
</center>
## Lesson 1.3 - Installing Visual Studio
Before we get started with the fundamentals of programming, we need to first establish what we will be using to actually write code. That’s what an IDE is used for! IDE stands for Integrated Development Environment, or in other words, a piece of software that allows you to write software, giving you a set of tools to boost your productivity, such as syntax highlighting, code completion, etc. Simple, isn’t it?!
In this book, we will be using Microsoft Visual Studio 2019 to write and execute our code. If you want, you can use other versions of Visual Studio, like 2017 or 2015, but if you want to follow along with this book, it is recommended that you use 2019.
**Installing the IDE**
To get started head over to https://visualstudio.microsoft.com/downloads/ and click “Free Download“ underneath Community edition. You can use Enterprise or any other versions, but for the sake of this tutorial, we’ll use Community.
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116857/vs-dl-page_ql47jy.png"></img>
</center>
<br>Once that has downloaded, open it up and you should see a screen with a list of components. You don’t need all of these components, just the “.NET desktop development“ one for now.
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116878/net-comp_jguce1.png"></img>
</center>
<br>Go ahead and click install after checking “.NET desktop development“. This will install all the core features of Visual Studio, and install support for C# development.
What did we do in this lesson?
We installed Visual Studio 2019, ready for some actual programming!
**Tasks**
1. Go ahead and get used to Visual Studio.
*Notes*
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
## Lesson 1.4 - Creating your first project
In order to start programming, we need to first create our environment, or to put it simply, our project. Visual Studio provides us with a handful of pre-created templates for us to use, for the sake of this tutorial, we will be using the simple Console App template. This template provides us with a simple command line interface, that looks a bit like the Windows Command Prompt.
*Task: Open Command Prompt on your PC. Note how the application looks.*
**Starting a new project**
Open Visual Studio 2019. You should be greeted with a page like this:
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116883/devenv_f6nhln.png"></img>
</center>
<br>We are faced with a few options:
**Clone a repository**: This allows you to edit code stored on a repository. This will be explored later on in Section {x}.
**Open a project or solution**: This one’s pretty self-explanatory. It opens a project that you have already created. Visual Studio uses the .sln file format.
**Open a local folder**: This will allow you to open a folder on your hard drive, so you can edit the files within it.
**Create a new project**: This option allows you to create a new project with a given template
**Continue without code**: This will open Visual Studio, but will not open any projects
<br>For this lesson, we will be using the fourth option, “Create a new project”. Once clicked it should show a screen like this:
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615117441/templates_cnqafq.png"></img>
</center>
<br>Using the search function at the top, type “console app”. As you start typing, you should see the results get added to the panel below. As mentioned before, we will be using the Console App template, so the option we are looking for is Console App (.NET Framework).
**NOTE**: Make sure to choose the one with the C# icon, not the VB one. VB (Visual Basic) is a different language from C#.
Once you double click that option it will show you a project configuration screen:
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116872/settings_q3utwq.png"></img>
</center>
<br>In the Project name text box (marked with a 1), type something meaningful, like “MyFirstApp”. To choose where to store the location of the project click the button with the three dots (marked with a 2). It is recommended that you create a folder on your harddrive called “C# Projects”, so you can store all your projects in one location. The Solution name is what you want to call the solution, this should be the same as your Project name.
After you have entered all those details, go ahead and click “Create” (marked with a 4). Visual Studio will then create a new project, based off of your configuration, and choice in project template (console app).
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116881/ide_cnlqvf.png"></img>
</center>
<br>That should be it! You have created your very first Visual C# project! The above image shows you what your screen should look like. It might seem daunting at first, but don’t worry, we’ll go through everything you need to know about this screen.
**The Solution Explorer**
On the left of your IDE window, you should see a tab called Solution Explorer.
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116869/solution-explorer_ebukfm.png"></img>
</center>
<br>This is the tab that shows all the files within your “solution”. If you can’t see this tab, go to View, then click Solution Explorer. One thing to keep in your notes is that a “solution” is a collection of projects. We’ll get to what this actually means in another lesson.
As you can see in the screenshot, the project has a couple of files. The one that is most important is the file called “Program.cs”. This is the file where we will be writing our code.
**The Text Editor**
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116866/text-editor_s24cxn.png"></img>
</center>
<br>In the middle we have the text editor (see above). This is where we will be writing our code. By default, we have the Program.cs file open.
At the top of the window we have a set icons:
<center>
<img src="https://res.cloudinary.com/dbps1c9ha/image/upload/v1615116885/bar_bdw4gn.png"></img>
</center>
<br>Most of these are pretty self explanatory, such as “Open” or “Save”. During the next few lessons you will mainly be using:
1. **Save all:** Saves all files in a project.2
2. **Start:** Builds and runs your program.
Go ahead and click the “Start” button. Note that nothing seems to happen, this is because we haven’t actually written any code. We will be doing that in Section 2.
What did we do in this lesson?
We created our very first console app project!
We explored all the necessary tabs in Visual Studio!
**Tasks**
1. Create a new Console App called MySecondApp. Store it in the same directory as MyFirstApp.
*Notes*
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
# Section 2 - Basics of Programming
## Lesson 2.1 - A brief introduction
You might be wondering “When will I actually start writing code?”, and the answer is: right now.
We’ve got everything set up, ready to start writing some C# code!
Let us start by the code automatically generated by Visual Studio upon the project’s creation.
**Analyzing automatically generated code**
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFirstApp
{
public class Program
{
public static void Main(string[] args)
{
}
}
}
```
There’s a lot to unpack here, so let’s get this started!
A programming language ie. **C#**, **C++**, **Python**, is just a set of tools that allow you to communicate with your computer.
Programming languages have a reserved set of words called keywords, ie. for, int, void, double, that produce a defined behaviour when used. Keywords cannot be used for naming variables, functions, etc...
It’s also noticeable that almost every line of code terminates with a semicolon.
We’ll talk about this more in depth later.
**Namespaces**
In C#, the project source code (the code that constitutes a project) is grouped into one or several namespaces, with the intent of making the source code more organized. In this case, the project’s source code is stored in the namespace “MyFirstApp”.
In order to access namespaces we use the using keyword.
Ever had a dossier? If so, probably you used separators at some point.
Think of separators as your namespaces and the content within the separators the actual source code.
As explained before, namespaces can be accessed through the using keyword in the following syntax: using NAMESPACE; so let us look at the first statement.
```csharp
using System;
```
It’s easily deducible that we’re using the namespace System or, in order words, we’re telling the programming language we need access to the code within the namespace System.
Namespaces can also be chained, meaning you can have as many namespaces you want within another namespace.
The next using statements are an example of chained namespaces.
```csharp
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
```
## Lesson 2.2 - Hello World!
In this lesson, you’re going to write your first line of code! Open your “MyFirstApp” project (double click the MyFirstApp.sln file). If needed, open the Program.cs file from your solution explorer. This is the file that will hold all your code for now. In the future, you may have multiple files that do different things. It all depends on the size and scope of your project. Let’s not worry about this for now, we’ll go more in depth about it in a future lesson.
For this lesson, we’ll keep it simple. We’ll be making the legendary “Hello World” application. The “Hello World” project is what most beginner programmers start off with. For most, it’s the first line of code they’ve ever written.
In the text edit, insert the following code:
```csharp
using System;
namespace MyFirstApp
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
}
}
}
```
So, what does this code do? Well, it's pretty self-explanatory. The line `Console.WriteLine("Hello, World!");` writes 'Hello, World!' to the console. If we go ahead and run this code, we'll see that it seems to do nothing. This is because we're not waiting for user input or anything like that, so the program writes 'Hello, World!', but then closes the program immediately, giving the effect that it isn't doing anything. In order to fix this, we need to include the line `Console.ReadLine();` beneath it, like this:
```csharp
//Your code goes here
Console.WriteLine("Hello, world!");
Console.ReadLine();
```
`Console.ReadLine();` waits for the user to type something into the console until they press enter. If you run the code now, you'll see that the phrase 'Hello, World!' has been written to the console, and the program will now wait for you to press enter before closing.
**Tasks**
1. Try writing a bit about yourself! For example your name, age, occupation, etc. Try doing this with multiple `Console.WritLine();`
*Notes*
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
_________________________________________________________________
## Lesson 2.3 - Variables
You're probably already familiar with the concept of variables. Variables are one of the most useful concepts in programming: They allow you to reference a certain value by a name that you can choose.
The syntax is as simple as this:
```csharp
DATATYPE VARNAME = VALUE;
```
Where:
+ **DATATYPE:** The type of data that the variable holds (such as `int`).
+ **VARNAME:** The variable name. Must obbey to a set of rules that will be mentioned later.
+ **VALUE:** The value that the variable will hold. Must be consistent with **DATATYPE**.
If we were to create a variable of type `int` (integer), the syntax is:
```csharp
int var1 = 25;
```
In this example, we assigned the value `25` to a variable called `var1` of type `int`.
What if we wanted to show the user what the value of `var1` is? Simple! Just use the previously-explained `Console.WriteLine();` statement!
```csharp
Console.WriteLine(var1);
```
This will output the value of `var1` that, in this case, is equal to `25`.
**Variable naming rules**
Variable naming **must** obey a set of naming conventions.
+ Variable names cannot start with number.
+ Certain special characters, such as ", ', ., $ cannot be used in a variable name.
+ Make sure to give your variables a descriptive (and short) name, in order to make your code more readable