An introduction into programming and writing our first lines of code
Introduction
Oh, hello there! I think you’re interested in learning C# (pronounced as “See Sharp”). Well, you’re in luck because ANF is planning to make tutorials and write some tips on C#. For those of you who like to interact through video, no this series will most likely never be based on “video”. There shall be nothing but plain text to read. So I hope you like reading!
Before we get into this, I want to give you a quick disclaimer that I am no C# professional and I am only really just going to teach you everything that I know. I will try to make everything as detailed and as simple to understand as possible. A quick note that if something is incorrect, you can hit me up at my Discord at discord/ANF-Studios (you can also get some help from other cool programmers there 😉) or just create a PR at the repository; github/ANF/anf.github.io.
So let’s get started, shall we?
Well, there might be people here with different reactions:
-
The beginner, who asks;
- What is programming and what is its extent?
-
The novice, who says;
- Time to write something that works.
-
The professional, who says;
- Stand back, time to write a game engine, an operating system, make a computer, and whatnot.
The beginner has the right thought. So let’s first get to know about programming itself, to know what we’re dealing with. I’m going to give you a few seconds to think it out yourself. Ask yourself, “what is programming?”. It’s okay if you can’t define it, just tell yourself whatever you know. Now let’s compare it with the actual definition according to Wikipedia:
Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a specific task.
Automation, to make our lives easier. That is what we’re going to learn.
Introduction into C#
So what is C#?
C# is a modern, object-oriented, and type-safe programming language.
Woah now, that sounds a bit complex and I can understand you. As promised; I will try to break it down for you. Okay so first of all; “modern”, what does that mean? Does it mean it has fancy keywords or is recently made? Well, no, not really. “Modern” programming means “less programming”. It’s like building your software at a higher level (as in not having to redefine every word in the English dictionary) allowing you to focus on what you want to make without having boilerplate code all over your codebase.
So that’s nice I guess? So ANF.. when will we start programming, teach me how to do that. I didn’t come here to get bored.
Well right, I’ll get to that in a sec, right after we complete this. Anyways, what does “object-oriented” mean? I’m going to give you a second to guess it out. It’s really simple, think of it yourself; “object”, what is an object? What’s its classification? Are you getting the hang of it? That’s great if yes, but let me further clear it out:
The term object-oriented means something, anything really, not specifically programming - forget programming for a second. Object-oriented means some classification. Try to think of it as an object classified in a category that is classified in another category that goes up to a limit. So let me give you an example, hopefully, it should clear it out. Say you have an apple, what’s it categorized in? Food, alright great food? But let’s be even more specific, what is it? A fruit, and from here and onwards, we can keep adding multiple “traits” to it and classify it such as can we eat an apple as a snack or not? Now in object-oriented programming, you have types, types are useful for telling C#; hey C#, this is my character, store this much memory (into the RAM) for it. Another example of this can be; hey C#, here’s a variable of mine, this is a number, an integer (which cannot have decimal points and takes a specific amount of memory) to be precise, allocate this much memory. That’s all there’s really to it.
We’ll be allocating variables a lot and this is an important concept. I’ll try to “allocate” more time to explain these because this will help you a lot 😉
Now finally, what is “type-safe”, well, type safety is.. well, a major problem, especially for beginners in more low-level languages where you control the memory which is uh.. not an easy task as a beginner. What’ll happen is that your application will just.. crash, that’s it. You have to figure out why it crashes, thankfully, Microsoft’s Visual Studio (it’s what we’ll be programming in 👀) has a built-in debugger that tells us as much as it knows. For example, when our application crashes, it will tell us which line caused it to crash and there we go! We can tell what’s the problem. That helps out, but this in C#, it’s a managed language which means pretty much everything will be handled for us which is kind of nice. Back to the point; C# handles memory for us in most cases which prevents such crashes and mishaps that you would see when dealing with other languages (even I am guilty of this, but it works out eventually).
Setting up our code editor
Now that that’s covered, let’s get to the fun part, shall we?
So what we’re going to be using is Visual Studio Code, it’s a “code editor” developed by Microsoft. There’s an IDE called Visual Studio 20XX
(where XX is the year), but I won’t be using that because it’s only for Windows and Macintosh, if anyone here is on Linux, they won’t be guided which is not what I want, I want my lessons to be covered for everybody, at least when it’s possible.
If you really want to use the IDE, you can find some other guide and use it, but I would only half recommend it.
For now, you can head over to Code.VisualStudio and press the download button (for me, I’m on Windows 10 x64, you might see something else, don’t worry about that, however):
Now you can wait for the installer to install, and as soon as that’s done, you can configure it according to however you want. However, I wouldn’t recommend changing any setting, they are the “best” ones or at least good enough. Beware that this “PATH” setting is really important, I would strictly prohibit you from leaving it unchecked. The “PATH” allows you to run the code
command in your terminal/CLI.
As soon as that’s installed, verify that it’s there - you might want to restart your PC just in case to be safer, but you do not need to unless it requires it.
After that, open up any terminal of yours, even command prompt will do and then type in code
and wait for VS Code (Visual Studio Code) to launch. You can also fire it up via your start menu or whatever you prefer though I would recommend getting used to the CLI as well.
Side note: You can also supply arguments to the code
command such as the path (not to be confused with PATH) argument which may look something like code ../
(go one directory before the CLI’s directory and launch VS Code in that directory). There are also other ones, type in code --help
for more information.
Now that you’ve opened your preferred folder within VS Code, you should see something like so:
Now yours will be most likely different from mine, don’t worry about that. I’ll explain the reason behind that; VS Code is highly customizable and there are these things called “extensions” which are like “Minecraft Add-ons” for those of you Minecraft players. There are almost no limits to how you want VS Code to look like, heck you can even change the colors of the interface.
You can have a look at some extensions that you can download yourselves by pressing Ctrl
+ Shift
+ S
. But for now, we will be installing only one extension, the C# extension which enables IntelliSense for us, so in the search box, type in “C#” and install the very first extension you see named nothing but C#
and is published by Microsoft.
Installing dotNET
Now that we have the C# extension, the extension (not VS Code itself) will install some tools that are required for debugging. While that is working, you also need to somehow convert your text-based “code” into some binary form to run it, that’s important because that’s just “intellisense” you have and that doesn’t have much worth without the actual tool to convert it, it’s called the C# “compiler”, that complies code and it’s called .NET (pronounced as “dot net”). And yes, you also need to install dotNET to build tools.
Now if you’re wondering what that .NET Framework was when updating Windows specifically, well, that’s the “runtime” framework that only allows you to run dotNET Framework applications. But what we’re installing is called dotNET, literally just dotNET. It’s nothing more.
Before we head over, I want to explain to you about .NET Framework, .NET Core, and dotNET.
- .NET Framework is a runtime framework that runs on Windows and is a bit older.
- .NET Core is a newer version of .NET Framework and runs better because the previous mistakes, or rather design flaws were now fixed but it does not matter if you ask me. dotNET Core runs on all Windows, MacOS, and Linux.
- .NET is so far, at the time of writing this, the newest framework which I highly recommend for you to adopt it and it does run on all Windows, MacOS, and Linux.
Alright, let’s get to installing it! Firstly, head over to dotNET.Microsoft/Download and select .NET 5.0 - or whatever is the latest recommended specification.
I don’t know how the screen looks like on 32-bit systems, but if you are on one, DO NOT install the 64-bit version, it would not work. And make sure to install the “SDK” and not the runtime, you don’t need that. This is because the SDK packs the runtime within it and also the development tools.
After that’s installed, once again, restart your pc just in case if you want to, I highly recommend it however just to stay safe. If it does ask you to, then you have no option at all.
Great! We have everything we need set up!
Creating our project
Now let’s move on to the fun part, open back up the VS Code window and click on the terminal, and type in dotnet new console
.
Now a few things, if you do not see the terminal, press Ctrl + Shift + `.
You can also press the new terminal button from Terminal -> New Terminal.
Now that you’ve run that command, you should see this or a similar output:
Now you will see a csproj file, a cs file, and an obj folder. Considering that you have .NET installed and the extension is running, it will ask you to “Generate Assets”, always press yes as you need those, they will be a time-saver so that you don’t need to set the project up.
Now you will see some folder(s), notably the .vscode
folder. That is responsible for telling VS Code how we want to run our application within VS Code.
Now every setting is nice, however, there’s this one config value I want to change, personally, I like when our application is launched in a new window instead of the same one. So what I’ll do is open up the .vscode
folder by clicking on it in my explorer (in VS Code) and open up launch.json
. Finally, this is how it looks, for me:
|
|
I want to change that "console"
’s value from internalConsole
to externalTerminal
And that’s it, we’re all set up and we are reading to code! You can close launch.json
and open up Program.cs
. What you see is some basic code:
|
|
What does our program mean
These are only a few lines however, it can be complex to understand for some of you. Forget everything in there, just focus on static void Main(string[] args)
. What that means is that Main
is the main method meaning it tells our program where to start running, or rather executing. Without the main method, programs don’t run as they don’t know where to start. About static, well that means that there can’t be more than one instance of that Main method, in programming, you can create more than one object. And that completely makes sense, for example, we have a “class” which is contains everything we can access user data, the class called User. We can access every variable in it, now it wouldn’t really make sense to make it static because there can be more than one user, marking it as static would only really just not allow having more user instances. That is a complex topic, we’ll leave that for another lesson.
Now void, what does void mean? Is it some portal to nothing? Well, kind of, but no, not really. void
means that our main method will not return anything, that’s all you need to know for now.
string[] args
is the argument of the main method. When we launch any application in our operating system, it may (or not) be launched with a few arguments, those arguments are stored there. You do not need to have it, you can freely remove string[] args
though keep note that you do need the brackets and you can change args
to almost anything you want to name it, unlike the Main method.
Inside our main method, there is this thing called Console.WriteLine("Hello World!");
. As you might have guessed, this tells our console; hey console, print whatever is between these two double apostrophes. Now, will it actually do that? I’m not going to say anything because it may or may not. There’s only one way for you to find out 👀
Running our code
You can press F5
to run our program and an external console should pop up and close immediately.
Well, why does it close?
That’s because we haven’t told our program to exit after it reads some key, or in other words, gets some input.
To do so, we will write Console.ReadKey();
in the next line like so:
|
|
Now, when we run our program;
It will only close when I press a key on to the console.
That’s great but our code will keep growing to a point where it’s hard to tell what everything does, for this purpose, there are these things called “code comments”. When we write something that isn’t valid code, we get an error:
So, to write a comment, there are two ways, the most basic one being putting to slashes (//
) before our text. The comment continues infinitely, it looks something like this:
Keep note that you don’t need to space it out, I just did that for readability.
Finally, another type of comment is made like /* */
with anything between these two marked as a comment. They look like this:
|
|
Now, for the next series, let’s comment everything out so that we don’t forget what each thing does:
|
|
Wrapping up
And that’s our first program!
With that being said, I think it’s been too long and you’ve spent some time setting this up, so we should wrap this up. I hope I made it as clear as possible, hopefully, this is everything you need to know before getting started.
So before leaving, I’ll be sometimes assigning some tasks to you so that you can test your one skills, or challenges if you will. I won’t be checking it unless you want to show it to me (hit me up at my discord server at discord/ANF-Studios 😉). For today, I wouldn’t give you something complex and it would be really simple.
Today’s challenge for you is to change the text within the strings of the Console.WriteLine();
. Try changing it, here are some examples:
|
|
Try and see them for yourself! Alright, see you!