C# Elementary1 Brave new worldString type introduction
Prev

A Warm Welcome

After I finished the second task to initiate self-destruction, the droid started to shake.
"RUN!” shouted Noname, but there was no need. I didn’t know that I could be so fast! 10 seconds later I was behind the nearest building, peeking around the corner.
But there was no boom. Just one little click and the robot simply fell apart.
"Now listen to me very carefully." Noname's voice calmly resumed in my head, "If you look at the building across the road, you can find the number 23 on it, written in blue paint. Right under the number, you will find the stairs. Go to the underground floor. Tell whoever you see there that Kevin sent you. They should know. You will be given another chip, some new clothes, and a USB stick with a virus. Do you understand me?"
"Yes, I’m not stupid, you know! Just because I don’t know C# yet doesn't mean you have to ask me after each sentence whether I understand or not."
"Yeah, sure, if it bugs you, I'll stop checking. Just get in there and get it done."
"OK, this sounds really easy."
"Oh, wait, one more thing: There will be no connection to me when you are more than 3 meters underground."
"That’s great, Noname. Do you mean to say that I'll be on my own when I get in there?"
"Exactly. But not forever, it is just a little break. And you can trust the people there."

It wasn’t hard to find the stairs. Going down them was a little harder.
I'm telling you, they were re-e-e-e-ally long.

Stairs

At the bottom of the stairs, I found an old soda machine that still worked. The display told me: “Choose your drink.” I looked at the drinks, and everything was normal, except… except number 23 was empty. The number painted above the stairs was 23, but it would be stupid to order a drink that wasn't there. So stupid… that a machine wouldn't do it, nor calculate that anyone would. Only a human would think of doing something so illogical. As soon as I pressed “23,” the floor — with me and the soda machine — rotated, and I saw that I was inside a room with a guy standing within arm's reach, staring at me.
"Who are you?" he asked.
"I’m new in the resistance. Kevin sent me. My name is Teo."
"Very nice, and I’m a machine king! Ah-ha-ha-ha!" he laughed. "Prove to me that you are one of us. Sit here, write code! Sit here or you will never sit again!"
"Be calm, Teo," I thought to myself, "things could be worse. This is definitely not a good time to panic."
"OK, OK," I answered in a more or less calm tone. I sat in the chair he was pointing at and read the task on the screen.

“Create a string variable, iSwear, with the value "I am one of resistance" and output it 5 times, each time on a new line.”
Shocked, I blurted out, "Hey, wait, I don’t know what 'string variable' means."
"Then I will kill you now, you are not one of us!"
"No, wait! I can learn!" It seemed this was a very good time to panic after all! "I just started an hour ago, I haven’t got to string variables yet, only integers. You know, like int x = 5. Give me a chance, please! Remember when you didn’t know about strings ?"
"I remember… and a machine wouldn't ask to learn like humans do. OK, listen. A string variable, or string , is a data type. Same as int , but unlike it, string represents text in computer memory. If you want to store and do other things with some non-numeric data, create a variable as string type and set it equal to the data, the same as you did with int but put the data in quote marks. Even if the data looks like numbers, if it's assigned to string variables, it will be treated as text."
He typed an example:

string someString; 
someString = "Computer, did we bring batteries? Computer?";

"Of course," he added, "as, with integer data, you can create the string variable and assign the data in one line, like so:"

string someString = "Computer, did we bring batteries? Computer?";

"Also just like int, after you have created a string and put data in it, you can output it to the screen like this:"

string someString = "output me 5 times!";
        
Console.WriteLine(someString);
Console.WriteLine(someString);
Console.WriteLine(someString);
Console.WriteLine(someString);
Console.WriteLine(someString);

"Outputs 'output me five times!' five times to the screen — without the quote marks, of course. Now you can try your task again — and you’d better not fail this time!"

"Is that good enough?" I asked. "Do you believe me now? I am human! I want to join the resistance!"
"Hmm… You are learning quite fast. Perhaps too fast." He was staring at me with suspicion. Then he relaxed a bit and continued: "But here, inside this resistance base, you need to work with strings all the time, and do more than just create and output simple strings. You won't even be able to open a door without knowing what string concatenation is."
" Concatenation ?"
"Yes. Sounds scary, huh? But actually it just means “ joining strings end to end ”."
"Are you stringing me along? What exactly does that mean?"
"OK. Maybe you are not learning as fast as I thought… Imagine two strings:" (He typed as he spoke so I could see exactly what he meant.) " “ I am happy to see you here, ” and “ Teo ” "Their concatenation is also a string: “ I am happy to see you here, Teo ”. The command or operator for string concatenation is a simple plus sign: “+”."
" You mean that concatenation of two strings is just adding one to the end of another? Then it is pretty logical to use a plus for it. "
"C# is very logical. Here is how our example would look in code:"

string begin = "I am happy to see you here, ";
string end = "Teo";
string result = begin + end;
Console.WriteLine(result);

Outputs:

I am happy to see you here, Teo

"Wow, I like it! Can I concatenate several strings? Stringing more strings together?"
"Absolutely. Look here."
He entered another example:

string begin = "All you ";
string middle1 = "need ";
string middle2 = "is ";
string end = "love!";
string result = begin + middle1 + middle2 + end;
Console.WriteLine(result);

Outputs:

All you need is love!

"I’ve got it now. When you explain, it makes total sense!" I said. Maybe his explanations were not all that great, but he had wanted to kill me five minutes ago, so I decided it would be wise to get on his good side. He responded warmly.
"That is awesome. Here is a little task for you to practice on."

"Well done, Teo! Do you have any questions for me before I let you enter our resistance camp?"
"If you had the same task, how would you solve it? Same as me?"
"Hmm… Almost. I would NOT create a separate variable for the result if I didn't need it. Instead, I would just concatenate the string variables in the output command like this:"

string begin = "All you ";
string middle1 = "need ";
string middle2 = "is ";
string end = "love!";
Console.WriteLine(begin + middle1 + middle2 + end);

"So I can output the result of “+” operation on strings directly?"
"Yes, you can. That’s what I’ve just done."
"And what about outputting the result of the other operations or types? Can I output, say, 24/2 or 12 * 3 or 4-2?"
"Yes, if you don't need to do other things with the numbers. It will look like this:"

Console.WriteLine(24/2);
Console.WriteLine(12*3);
Console.WriteLine(4-2);

"These lines will Output:"

12
36
2

He concluded, "That’s enough about strings for now. I’m sure you’ll learn more in the future." Then he waved his arm in a gesture showing me I should pass into the underground resistance camp.
I hesitated. "But... Kevin told me that you have something for me. Clothes, chips… you know."
He just repeated his gesture. I decided it would be best to trust and not be a pest, and moved in the direction indicated. There was a hallway that curved so that I couldn't see where it went until I was near the end.
I entered a big room full of people working on computers. They looked very busy, and nobody paid any attention to me. There was a door with a sign saying “Teo” at the opposite side of the room.
"Strange," I thought, "how long have they been preparing for my arrival?"
I went to the door and opened it. There was a wooden table to the left of the door, with a chair and a little bed in the corner. Minimalistic, but I liked it.
On the table I found a note saying:
“Dear Teo, welcome to the resistance base! I hope you enjoyed your journey with the program 'Noname' that I created to get you here from the time vortex. Take your time, get some sleep. You have so much more to learn and do! See you tomorrow!”
I lay on the bed, getting drowsy as many different thoughts whirled around in my head. "Noname is just a program? Does that mean I shouldn’t return to him? Is all this a lie? How long have I been awake? Or is this all a dream? How long can a person stay awake? Who is stronger: a lion or an elephant?" At some point, the thoughts became dreams, and I was sleeping like a child.

Prev

At CodeEasy we believe that learning should be fun and engaging. Please reach out to us to share your feedback and collaboration ideas.