Chapter 4: Variables.

Creating a variable and variable types.

one of the most important things in programming is variables, variables contain data, and in C#, variables are strongly typed, which basically means, when you declare it, you need to tell the compiler what will this variable contain, and once created, you can't put another type of data.

let's give an example.

int x = 5;
int y = 10;
int z = -10;

x and y are variables of the type int, integer, ints contain normal numbers, 1, 2, 3, we also have floats and doubles for numbers with fractions.

float x = 0.4f;
double y  = 0.123456789;

floats and doubles are similar, but doubles are bigger, can hold more decimal point numbers, and are more reliable. If you're not sure which one to use, go with double.

we also have bool(boolean) That can store 2 values only, true, or false.

bool isEnemy = true;
bool isFriendly = false;

now, IF you remember in our last chapter, when we explained

Console.WriteLine("Hello, World!)");

we said that "Hello, World!" Is a string, so you would wonder, how would I write it like the ints floats and doubles above?

Well, the answer is

string x = "Hello, World!";

so as you can see from our examples above, a variable has 3 parts.

  • Type: Type of the variable, string, int, float, double, bool, or any other type.
  • Identifier: Basically the name of the variable, there are a few rules for this, name must either start with a character or an _ (underscore) it could contain numbers an characters, but the name can't begin with a number.
  • The value, for example: 5, 0.4f, "Hello, World!".

Using and changing variables.

so now after we explained how to create variables, let's explain their uses.

since variables store data, we must be able to read them.

string greeting = "Hello, World!";
Console.WriteLine(greeting);

That's it, think of it this way, remember how I said that when you call the WriteLine method we give it an argument? The "Hello, World!" is a raw form of a value, but when we instead give it a variable, it knows that it's supposed to read it.

P.S. The raw form of a string is called a string literal in programming.

So, you would ask yourself, but if variables are, well, not variable and are instead constant and unchanging, what's the use out of them? In fact, that is not the case.

You can change a variable after creating one.

int x = 5;
x = 10; // We changed the value of x.
Console.WriteLine(x);

Ok, but that's still not practical, so let's do something more interesting, taking user input.

Console.ReadLine.

Some of you when hearing about the WriteLine method might have asked themselves, does a ReadLine method exist? Yes, in fact, it does, let's see how it works.

Console.WriteLine("What is your name?");
string name = Console.ReadLine();
Console.WriteLine($"Hello there {name}");

we have a few different things here.

  • We called a method to give a string a value, this happens because methods can return values, and we can assign them to variables.
  • In the string literal, we did $"Hello there {name}". This is a special type of string literal, see where we did {name}, that's basically slotting in the variable in the middle of a string. This can only be done with strings that are prefixed with $.

now, when you run the code, you should get a message that says what is your name, when you do, write anything in the console screen, then press enter.

then you should see your name being printed along with a greeting.

that's what ReadLine does, it takes the input from the user on the Console and when successful, it gives that value back to the caller.

Next, let's explore if, else if, and else.