Hey there, Gamers and Game makers! Welcome to a new tutorial series, An Introduction to C# in Visual Studio. In this weeks tutorial, we're going to be looking at what variables are in C#. The various types and how to use them. So, let's get started! First thing you'll need is to have Visual Studio installed with the C# language package installed along side it. Once you have done so, click New, then New Project and select Console App. For the purpose we'll be using a console application rather than a desktop application for ease of the tutorial. Go ahead and call the project whatever you like. You'll be presented with the a basic hello world program. Don't worry about what you see for now. We'll be only looking at variables for this tutorial. So, what exactly is a variable. You can think of a variable as a container for information and different types of variables hold different types of information. A variable will reserve space in memory for whatever type of information it's to store once it has been created, or declared, to use the proper terminology. What types of variables are there? The five most common ones you'll deal with are int, float,bool, string and char. The int and float variables represent numbers. An int variable deals with whole numbers only while a float can handle decimal numbers. A bool is a variable that stores a true or false value and a string is simply a string of text. The char variable stores a single alphabet character. Let's look at some examples. Starting with how to declare an int variable. I start by declaring the type of variable by typing the word "int" followed by a name for the variable.Since an int is used for whole numbers, I'm going to call this variable "num1". I then assign a value to the variable by using the "=" sign followed by the value I wish to assign which in this case is "2". I then end the declaration with a semicolon. It's very important you do this as you'll get an error otherwise. This is all well and good but what can we do with int variables? Let's say we have more than one int variable. We can use basic mathematical operations on them in order to say, return the sum of those int variable. Let's try doing just that. I already have one int variable, so, I'm going to create a second int variable called "num2" with a value of "4" and also create an int variable called "sum" that will store the sum of my other two int variables. As you can see, we have a variable that will store the sum of our two numbers, but, how do we actually get the sum of those two numbers and assign it to the sum variable? It's nearly as you would expect to do it on paper, we add the two variables using an addition sign. We say sum is equal to num1 + num2. Be aware that while in this instance the + sign is being used for addition, it can also be used for concatenation, which we'll get to at a later stage. Great! We now can add our two numbers together. However, we can't see the result at the moment. This makes it difficult to tell if the calculation is working and if it's working correctly. So, how do we display the result of our addition? We can actually borrow a line from the default hello world program for this. You may have noticed that line of code that says Console.WriteLine("Hello World!");Well, a Console.WriteLine() is used to display whatever is between the brackets to the console window. We can therefore use it to display the result of sum. To do so, it would look like this: Now, we're ready to run our console app for the first time and actually see the result displayed in the console window. Go ahead and click Start. As there should be no errors, the program should compile and you'll be given a window which looks like this: This same logic can be applied to floating point numbers or doubles. Try it out for yourself and see what kind of results you get! Let's take a look at strings. We declare strings much like how we did for ints. We start off with the data type which in this case is a string followed by a name for the variable. In this case I'm going to use the variable to store my name, so, I simply call it name followed by the string value of "Dan". If you use a Console.WriteLine(), you can print that value to the screen as you did before with sum. Now, remember I mentioned concatenation before? Now's the time we're going to use it. So what is concatenation when it comes to strings? Concatenation of strings is when we take two or more strings and join them together. They don't always have to be variables. I can write a string in my Console.WriteLine() along with a variable if I want and it'll display it in the console. For example: Let's take a look at a bool variable. As I've said before, a bool variable is used for storing a true or false value. This can be used for checking certain criteria. For example, if you are over 18 or not after entering you age for validation. A bool value could be used. Let's look at how we declare a bool. As with our previous variables, a bool is declared much the same. The keyword followed by a name and value. It's important to point out, you don't always have to assign a value when declaring a variable. For a bool for example, it may not make sense to assign true or false until after a certain check has been made. Defaulting it to say a true value might create a problem. I can display the current value to the console as before using a Console.WriteLine(). In the next tutorial, we'll look at using if statements.
Until next time! |
Archives
April 2019
Categories |