Learning to Code — Part 3a: Basic Concepts

Scott Rosenbloom
5 min readJun 28, 2018

--

In my multiple trips back and forth, and back and forth (and back and forth again), I now have a much better understanding of the different groups of things and how each one relates to each other thing. To get you caught up, though, I’m going to jump back to the beginning of the C# tutorials within the SoloLearn app.

The first section was just called Basic Concepts, and everything in there sunk into my brain just fine. That said, here are the topics that were covered:

  • First, they talked about the fact that C# runs on the .NET Framework. This is basically the environment that contains the Common Language Runtime (CLR), which manages the different aspects of the code when it runs, and the class library, which contains a whole bunch of built-in classes (with different functionalities), as I described before (which you can use in addition to the new ones you create).
  • Next, they went through variables, which are basically named locations in the computer’s memory that hold data of a particular type (i.e. a number, a word, a true or false value, etc.). You’ll need to determine and specify the name and data type to be used.
  • After that, they have you create your first program in Microsoft’s Visual Studio, which has a free version (although they do have a built-in environment that you can also work with in). You start a new program using the Console Application template, which uses a text-only interface. The most important thing they want you to know here is that every program must contain a method (or function) named Main, and that method is written like this:
static void Main(string[] args)

You can then hold down Control on your keyboard and tap the F5 key to run the application.

  • Next, it goes through printing text to a window, and officially ending a line of code. To print the line of text, you enter this:
Console.WriteLine(“Hello World”);

The Console part is saying to do the next thing I say in a window (or console). The period just means that the next thing I’m going to write is one of the functionalities of the thing I wrote before the period, and you should do it (in this case, write a line of text). Then, you put what you want to write in quotation marks, inside parentheses. Finally, to end the line of code, you put in a semicolon. It’s like a period at the end of a sentence. It’s worth mentioning here that a lot of times when I’ve run code and it doesn’t work, and shows me an error, it’s because I forgot to put in the semicolon.

After that, they go through initializing (or creating) a variable and setting it’s type and value at the same time (and even do it for more than one variable). Jumping back to the previous line of code, instead of “Hello World”, they show you how to pull the values of those various variables (say that three times fast) more quickly, by using variable placeholders like this:

Console.WriteLine(“x = {0}; y = {1}”, x, y);

What this code is saying is first I’m going to tell you the text to print to the window, but because I’ve written {0} and {1}, look what I wrote after that to tell you which variables from which to take the values. In other words, print x = to the screen, and swap out the {0} with the value for x, and then print ; y = to the screen and swap out the {1} with the value for y. Needless to say, you can do this for more than 2 variables.

  • After that, it goes into getting input from the user. This was really the first time I felt like I was creating something that someone could use. Anyway, since the thing entered by the user needs to be stored somewhere (which you’d name something) so you can use it again later, you need to create a variable. For efficiency’s sake, though, you can ask for the value and set it equal to a variable all at the same time, like this:
yourName = Console.ReadLine();

I really like how ReadLine is so similar to WriteLine. It makes it very easy to remember. An important thing to note, though, is that ReadLine is going to take in the value as a string (or word). If you’re going to want to use that value to do something other than what you can do to a word (like add or subtract numbers), you need to convert it to whatever data type you need. You do that with this code, Convert.ToXXX, where the XXX is whatever data type you want to convert to (like an integer or true/false, also known as boolean). Again though, for the sake of efficiency, you can do all of this in a single line of code:

int age = convertToInt32(Console.ReadLine());

So, this is establishing a variable called age and setting it to be of the integer data type. Then, it’s converting the string value that the user enters (because of the ReadLine part of the code) to an integer. It’s worth mentioning that before this line of code, you can use the Console.WriteLine line of code to print to the screen some instructions for what you want the user to enter.

Putting this all together, it would look like this:

static void Main (string[] args) {
Console.WriteLine(“What is your age?”);
int age = convertToInt32(Console.ReadLine());
Console.WriteLine(“Your age is {0}”, age);
}

This took me through the first half of the basic concepts. In my next post, I’ll go through the rest, which taught me about:

  • Commenting out lines of code,
  • The var Keyword,
  • Constants,
  • Arithmetic Operators, and
  • Assignment and Increment Operators.

As usual, if you’re in the know, please feel free to correct anything that I’ve written. It’s more important that I (and anyone else reading this) understand it correctly than anything else.

Also, here’s a link to the previous post, Learning to Code (and Learning how I Learn) at 42 — Part 2.

--

--