C# Variables for Absolute Beginners

Hello Guys, today we will be talking Variables and Types.

So what really is a variable? Or a type. I will explain this using my favorite example.

Imagine you need to store up water, maybe you live in an area where your water supply isn’t always sure and so you need to store up for times when there is no running water. There are two things you need to ensure that you’re not stranded when there is no running water.

The first is the Water you actually need to store up and the second? A container to store the water in, which can be a drum.

Let us go back to variables. Variables are containers that hold value. We can use this value at a later time. It is important to note that there are two items in play here too; a container (variable) and the thing it holds (a value). In a software application, we will often need to store certain values, a person’s date of birth and phone number can be stored in the application, these values will be used to send them a happy birthday message when it’s their birthday. The software just checks the stored records every day for who’s birthday it is, retrieves their phone number and sends them an automated text message. (This is actually how most real-life systems work).

So bringing together all I’ve said. What are Variables?

Variables are the containers used to store values that will be needed later when programming.

Wait, but we are supposed to be discussing variables and types.

We understand what variables are, what are types?

Once again we will return to our dear Water Example. As we said before, we need to store up water, and we need a container. But what kind(type) of a container do we need? We obviously won’t store water in a basket. We need a drum. It would also be unwise to store shoes in a drum (even though we can) we should use a wardrobe or a shoe rack.

In the same way, in programming, different types of values are stored in different types of containers. The types of values in a computer program will not be water and shoes though. Computer programs store the following types of values:

Text: an example of text includes your social media status updates. All the words you type are saved as text in the computer software. They are stored in a type of variable(container) called the STRING

Numbers: When you enter your age in a computer (say 15 years old) it is stored as a number. Numbers in computer memory are stored in a type of variable (container) called INT.

Decimals: Sometimes you are making a banking application and every last kobo matters. You don’t want to tell a customer they have 2 Naira when they actually have 2 Naira 50 kobo. 2.50. for situations where you need to save decimal numbers, you use the DOUBLE type.

We have heard enough grammar, let us now use some of these variables, or containers so you will get a better understanding.

Before we begin, I will give you a magic line of code. It helps you to display information to your users.

This line of code is Console.WriteLine();

Ignore that for now. And let us proceed.

We will create a few variables now and save something inside of them.

Tip: Create a new console application and follow this example.

First, let us create a variable to save my name. We need to create the container and put my name inside. We do that using the line of code

string personsName = “Vincent Nwonah”;

Let us create another variable and save my age inside.

int personsAge = 900;

Variable Declarations

Let us take a look at these two examples.

The first word on each line tells the system what type of variable we want to create, it is a way of telling the computer what type of information we will store in the variable.

In the first example, we are telling the system that we are creating a variable that will save text — A person’s name, and the second example we are saying we will save a number — a person’s age.

The next word on the first line is myName. This is the name of the variable. The name of the variable is necessary so you can use it later in the program. Imagine you have stored water in different buckets, and you need to tell someone to bring a specific one to you, you may say; I need the water in the GREEN BUCKET, you are in essence naming that bucket because you need a way to refer to it. In the same way, variable names are necessary so we can refer to variables later.

Next in line is the equality sign and my name. On that line, we are saying: take this piece of information and save it inside this container I have created.

Finally, let us display the values we have saved in out variables. we do this using that special code: Console.WriteLine(); So, to see what’s in our variables, we say Console.WriteLine(variableName); To see what is in personsName therefore, we have Console.WriteLine(personsName);

Your final code window should look like this. Ignore the “Console.ReadLine();” for now. Run the code and see your values displayed!

If you have any questions shoot me a mail at vnwonah (at) outlook.com.

--

--

Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store