C# Basics – Variables and Data Types – Part 4

We explore how variables work, in C# programming language, in this tutorial.

Andraz Krzisnik
C# Basics – Variables and Data Types...

Variables and Data Types

Software works with data that’s stored in the memory. All this data, to be usable, needs to be stored in some defined space. We name these spaces variables. Variables can have their values changed, overwriting the last one it held.

There are many different data types so, we can create different variables, such as integers and strings.

Every variable needs a name, so we can call it throughout our code.

string s = "Here is some text";
int i = 35;

When we create our new variable, we also need to declare it. This means we assign it a value.


When we have a variable that needs to be denoted as integer, we type int in front of the variable name, so this variable can hold only integers and no other data type values.

We can still normally use operations such as summing, subtracting, multiplying. But when it comes to dividing there is something you should know.

Since the result of each operation will be an integer as well, these results won’t be accurate, because all numbers beyond the decimal point will be erased.

So for example if we tried to divide 5 by 3, our result will be 1. There is also one more operator that can come in handy if we need more accurate results than that.

The % operator will give us the remainder. So basically if we try 5 % 3, the result will be 2, since 5 / 3 is 1 and the remainder is 2.

int a = 5 + 3;
int b = 5 - 3;
int c = 5 * 3;
int d = 5 / 3;
int f = 5 % 3;

MessageBox.Show("Sum: " + a.ToString());
MessageBox.Show("Subtract: " + b.ToString());
MessageBox.Show("Multiply: " + c.ToString());
MessageBox.Show("Divide: " + d.ToString());
MessageBox.Show("Remainder: " + f.ToString());

Doubles – Real Numbers

To create a variable which will hold a real number we need to type double before the name of the variable. These kind of variables can hold numbers with decimal point, so it can be much more precise than integer type variable.

double r = 2.667;

Example above will display a window with showing us a real number.

Math Class – Functions

We can find functions for more complex operations like trigonomic functions, rounding, square root and more in this class. When we call it we have to write down class name first followed by a dot (.) and finally select the function from this class we want to use.



  • PI – number ?


  • Abs – absolute value
  • Acos – arcus cosine – angle (in radians)
  • Asin – arcus sine
  • Atan – arcus tangent
  • Cos – cosine of an angle
  • Max – returns the largest of two numbers
  • Min – returns the lowest of two numbers
  • Pow – exponentials (first number is the base, second is the exponent)
  • Round – rounds to the closest integer number
  • Sqrt – square root
  • Tan – tangent
  • Truncate – returns a whole number by cutting decimals off

Converting Between Data Types

There may be many occurrences when you will need to convert a value from one data type to another. Some can be converted automatically, another are converted with code.

Following example will show you how to convert a number value, of type integer to a double.

int a = 8;
double converted = (double)a;

Here is another example, this one shows how to convert string type to integer.

string n = "5";
int m = Convert.ToInt32(n);

How to convert from string to a double

string j = "5,6";
double k = Convert.ToDouble(j);
Download Examples Next Chapter
Show Comments (0)


Related Articles

C# Basics

C# Basics – Random Numbers, Chars and Strings – Part 7

Random Numbers To generate a random number, we will use a constructor called Random. Then to generate a random number we will use either .Next() or .NextDouble() function. Random...

Posted on by Andraz Krzisnik
C# Tutorial

C# Tutorial: How To Create Image Zero Padding

Zero padding an image is useful when we’re convolving it with a filter of certain size. How much padding should we use depends on how big our filter is. What is the purpose...

Posted on by Andraz Krzisnik