In this post we will cover some basic value types that we use in our everyday work, such as int, double,and bool.
Before we begin with data types you should be familiar with variables. Variables are nothing more than name given to a memory that our programs can manipulate. Value types store values inside variables that are stored in side stack. Stack and heap will be covered in post where we will compare Value and Reference types. For now, all you need to know is that every value type variable has its value stored in its own place inside memory, and when you assign value from one value type to another you create copy of that variable.
By now you must have heard that you must Assign variable, Initialize variable and declare variable. What do these terms stand for?
First of, variable can be initialized
1 2 |
//datatype followed by a name we wish to give our newly created variable bool myBool; |
Secondly, variable can be declared
1 2 3 4 5 6 7 |
//name of variable that we wish to declare //= //then we call default constructor for bool type myBool = new bool(); //or simply bool = false; |
Lastly, variable can be assigned value
1 2 3 4 5 |
//we create a new variable of type Boolean and call default constructor for bool type var bool = new bool(); //or simply var bool = false; |
Basic mathematical operations with value types
(Addition, subtraction, multiplication, and division)
Be warned that it is recommended to use same data types when performing these kind of operations, soon you will find why.
Addition
Lets start by adding two integers together.
1 2 3 4 5 |
int firstNumber = 1; int secondNumber = 2; int result = firstNumber + secondNumber; Console.WriteLine(result); Console.ReadLine(); |
Try running this code and result will be “3”.
Now try changing secondNumber value to 2.2, you will get compiler warning “Cannot implicitly convert type ‘double’ to ‘int’.” Following actions can be performed depending on your desired outcome:
I want my result to be an integer:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int firstNumber = 1; int secondNumber = (int)2.2; //we cast number 2.2 into integer data type int result = firstNumber + secondNumber; Console.WriteLine(result); // result will be 3 Console.ReadLine(); //Alternative option int firstNumber = 1; int secundNumber = Convert.ToInt32(2.2); // we use Convert.ToInt32 Method to convert value to integer data type int result = firstNumber + secondNumber; Console.WriteLine(result); // result will be 3 Console.ReadLine(); |
Difference between these two approaches is that first one will always throw Exception but second one will throw exception only if value is greater than Int32.MaxValue (2,147,483,647) or less than Int32.MinValue (-2,147,483,648).
I want my result to be an double
1 2 3 4 5 |
int firstNumber = 1; double secondNumber = 2.2; //we must change datatype of our variable to double double result = firstNumber + secundNumber; // because we want our result to contain decimal values we have to change this variables datatype to double Console.WriteLine(result); //result is 3.2 Console.ReadLine(); |
Subtraction, multiplication, and division
Same as addition, but instead of “+” operator we use “-“, “*” and “/” operators.
Thank you for following me this far! Our next lesson will be Eumerations.