I woke up because of people talking next to my bed. One of them took two steps in my direction.
"Hi, my name is Ritchie. I am the leader of this resistance base. Do you want coffee?"
"Oh yes! I would like some coffee. And a little bit of explanation. Is Noname a program? Did you create it?" I had lots of questions in my head.
"Yes, Noname is a program, and yes, I wrote it," Ritchie answered calmly. "One of Noname's purposes was to bring you here."
"OK, what are our plans then?"
"We need to prepare you for tomorrow's mission." His answer was short and precise, but not informative enough.
"You say mission… In my current state, I'm not up for any missions. Do I have a choice to avoid it?" I asked hopefully.
"Absolutely no chance. The mission starts tomorrow at 11:00 pm," Ritchie said with a note of finality, "and here is your coffee, by the way," he added, passing me a cup.
The coffee was not that good, without milk, and probably very old, but it was still coffee. Ritchie continued:
"You need to understand how computers are built to have at least a basic understanding of how programs work. Are you ready to dive in?"
"Give it to me!"
"The main function of any computer (PC, laptop, tablet, smartphone) is to calculate numbers and store the results. It is not much more than that! Calculate and store."
Ritchie was pulling out parts of the computer and showing me one by one.
"Storage devices usually look like this:"
"It all depends on memory and device type. We are not going to dive deeply into this right now, but it is good for you to understand that when you use computer memory — it is one of these pretty devices," explained Ritchie.
"A processor makes the computer's calculations. It looks like this:"
You have more than one processor in a computer, but we will mostly work with the CPU, which means Central Processing Unit. Something like “the main guy” in a processor society."
Ritchie continued, "Programming is very abstract, especially in 2113, and I want to make things as clear as I can for you. That’s why I am showing you these things, in order to bind your knowledge to something that you can see. If you enter the pilot's cabin in a plane you will see a lot of different devices, right? But to use them, you need a pilot. In computers, the pilot is an operating system. An operating system is a significant and smart program that combines the work of all the hardware devices and gives you the possibility to work with a computer. The most popular humanly-created operating systems are MacOS, Windows, Linux, and Android. Have you heard about them?"
"Who hasn't? Don’t ask me stupid questions, please. Are you sure I need to know this?" I asked skeptically.
"Does a dermatologist need to know the structure and functions of the stomach?" Ritchie inquired.
"Of course! Skin problems could be connected to problems with the stomach," I said.
"It's the same with programming. Everything is connected. That's why, to keep you on your toes, I want you to complete this assignment, to polish the skills you already have", Noname exclaimed.
"By the way, I’ve got news about the police drones. They are scanning that sector less often. You were very “lucky” to meet them! Did you enjoy using C# to reprogram drones?"
"Yes, but there was no other option."
"Hm… You could actually choose any other language… But C# has so many advantages that I totally understand your choice," Ritchie smiled.
"What advantages?" I wondered.
"OK, here are several of C#'s advantages that I would like to mention:" He ticked off a list on his fingers...
Easy to learn and widely used. With C# it is possible to write games, desktop applications, websites, and services.
C# has a big community and helpful documentation.
Has a lot of solutions “out of the box.” No need to solve many tasks that were already solved before you.
Cross-platform — using the technology of .Net Core, C# programs run on various operating systems and even on different devices: desktop computers, laptops, phones, etc. You can write applications for Android, iOS, Windows, and Linux; and you can create websites in C#!
"Can you give me an example of an “out of the box” solution in C#?" I asked.
"Here you are... In many other programming languages, to concatenate int variables and strings, you need to use a plus sign (+), like this:
int applesCount = 5;
string result = "I have " + applesCount + " apples";
The output is: I have 5 apples
If you concatenate an int variable and a string — you will get the integer number converted into a string and then concatenated with the whole string.
"Just the same as string concatenation, including the int variable in the string?" I asked.
"Yes. This looks nice, unless you have many variables and a lot of concatenations:
int applesCount = 5;
int orangesCount = 10;
int bananasCount = 3;
string result = "I have " + applesCount + "apples, " + orangesCount + "oranges and also " bananasCount + " bananas.";
Console.WriteLine(result);
Now the output is:
I have 5 apples, 10 oranges and also 3 bananas.
"Do you like how the code looks now?" Ritchie asked.
"More or less, but now it is much harder to visualize the final result, because of the separate chunks of strings joined with plusses," I replied.
"I think so, too! C# gives the option to rewrite that code snippet this way:
int applesCount = 5;
int orangesCount = 10;
int bananasCount = 3;
string result = $"I have {applesCount} apples, {orangesCount} oranges and also {bananasCount} bananas.";
Console.WriteLine(result);
Outputs:
I have 5 apples, 10 oranges and also 3 bananas.
"How do you feel about this code now?" Ritchie asked.
"It is more readable because the concatenations are accomplished with nothing more than curly brackets. I can read it as a sentence!" I exclaimed.
"Exactly. This is one little example where C# has a built-in mechanism to make your life easier! This technique of injecting integers (and strings and other types as well) in strings is called strings interpolation. You can read more about it here.
To use strings interpolation, you should start your string with a dollar sign $ and put every variable that you want to insert into the string in the curly brackets: {variable}.
Now it is time to practice. Convert the caffeine in your blood into code, as many programmers do! Here is your first assignment:"