### C# Basics – Variables and Data Types – Part 4

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

Back to Latest Articles
###
C# Basics – Variables and Data Types – Part 4

###
C# Basics – Comments – Part 3

###
C# Basics – Writing Code – Part 2

###
Intro – Start Programming – Part 1

C# Basics

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

C# Basics

Let's take a look how comments in C# programming language work.

C# Basics

Let's create our first application with Windows Forms Application

C# Tutorial

Intro – Why is programming so important? There are so many people that get discouraged from programming, because it may seem too difficult. It’s actually way simpler than it...

C# Basics – 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());
```

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;
MessageBox.Show(r.ToString());
```

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

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.

Constants:

- PI – number ?

Functions:

- 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

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

## Comments