In my last post , I showed you how to make a basic C# program and I briefly introduced variables so that we could capture input from the user. In this post we will take a deeper dive into what variables are, and introduce some of the basic data types.
Variables
A variable is a way of storing a data within your program so that it can be accessed again later in the program. Each variable that you create can only hold data of a particular type, and this type is defined when the variable is created. For example, if you create a string
(text) variable, you cannot store number data in that variable.
Declaring and assigning variables
Creating a new variable is known as declaring a variable. When declaring a variable, the variable is given a data type, a name, and optionally some data. Putting data into a variable is known as assigning data to that variable. You can assign data to the variable when it is created or later in the program.
Please see below the 3 ways that you could declare a variable:
string greeting;
string greeting = "Hello, my name is Sam";
var greeting = "Hello, my name is Sam";
In the first case, we have created a variable called greeting
, which has a string
data type. We haven't assigned any data to it, so we say that this is an unassigned variable.
In the next example, we have also declared a string
variable called greeting
, but this time we are also giving it some data. The assignment operator (=
) takes whatever data is on the right hand side ("Hello, my name is Sam"
) and assigns it to the variable on the left hand side (greeting
). Because we have declared that greeting
must be a string
, the data on the right hand side must also be a string
.
In the last example, we a using a new keyword, var
. This keyword is used to implicitly assign a data type to the variable based on the data that you give it. In this example, we are trying to assign a string
to the variable, so greeting
will still have a data type of string
. It is a matter of preference whether or not you want to explicitly define the data type, or to let C# work it out implicitly. In my own code, I prefer to use the var
keyword, but in these tutorials, for the sake of clarity, I will continue to declare the variable explicitly.
Once a variable exists, the data inside it can be changed by assigning a new value. However, the data type cannot be changed. For example, if you declare a variable as a string
data type, it can only ever hold string
data. If a variable is already declared, you don't include the data type in the line of code.
string fruit = "Apple";
fruit = "Orange";
In the first line, the variable fruit
is being declared and assigned at the same time. In the second line, because we have already declared the variable, we can just write the name of the variable and the assignment.
Basic data types
According to the documentation , there are 16 different data types that C# has built in to the language. I say "built in" because it is also possible to define your own data types, which we will cover later in this course.
Here we will cover some of the most commonly used data types that you will mostly likely come across in your day-to-day usage.
1. string
and char
We have already covered strings quite a lot previously, but this data type allows you to store text data in a variable. You show that some text is a string by wrapping it in quotation marks.
The char
data type is similar to a string
in that it allows you to store text data. However, it only allows you to store a single character. You show that a character is a char
by wrapping it in apostrophes.
string myString = "Hello, I'm a string";
char myChar = 'a';
2. int
and float
These data types allow you to store numbers in them. An int
only allows integer numbers to be stored, whereas a float
allows numbers with a decimal point to be stored.
int myInt = 5;
float myFloat = 5.829;
3. bool
A bool
(short for Boolean) allows yes/no data to be stored. A Boolean variable can have one of two values; true
or false
. They are useful for controlling the flow of a program, which we will cover later in this course.
bool isTrue = true;
bool isFalse = false;
Conclusion
In this post, we have taken a deeper dive into how to declare and assign variables. We have discussed that variables can update the data held within them later in the program, but their data type must remain the same. Five of the most commonly used basic data types have been introduced.
To make sure that you don't miss any posts in this series, please follow this blog and subscribe to my newsletter. If you found this post helpful, please like it and share it. You can also find me on Twitter. If you want, you can also buy me a coffee ! 😊
This post is part of a beginner C# series. I will update this section with links to each post when each new post is published.
Top comments (0)