Navigation

Related Articles

Back to Latest Articles

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.

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

Integers

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.



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.

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.

 

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

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.


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

How to convert from string to a double

Download Examples Next Chapter
Show Comments (0)

Comments

Related Articles

C# Basics

C# Basics – Exception Handling Statements – Part 9

Exception Handling Statements Sometimes, there comes a situation that where our code might crash. We can handle these kind of situations with exception handling statements. By...

Posted on by Andraz Krzisnik
C# Basics

C# Basics – Object Arrays – Part 13

Object Arrays We already mentioned before, that when we create a class we create an object with custom data type. So, as you can see, like strings, integers and other data types,...

Posted on by Andraz Krzisnik