### 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

### Histogram Equalization And Magic Behind It

### Intensity Level Slicing With C# – Explore Image Processing

### C# Tutorial: Contrast Stretching with Normalization

### C# Tutorial: How To Use Log Transformation on Images

### C# Tutorial: How To Flip an Image Horizontally

### C# Tutorial: How To Scale Images Without Deforming Them

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# Tutorial

C# tutorial on how to achieve histogram equalization on an image.

C# Tutorial

In this tutorial we will be talking about intensity level slicing in image processing. What is Intensity Level Slicing It’s a process that highlights pixels in an arbitrary...

C# Tutorial

This post is a short revision of Contrast Stretch post we already worked on in the past. Where we talked about histogram equalization, which is a little more complex method than...

C# Tutorial

What is Log Transformation Anyway? Log transformation in image processing is a part of gray level transformations. It works by transforming each pixel individually. It is usually...

C# Tutorial

Where Do We Start? There are numerous ways you can flip an image horizontally. It’s pretty basic functionality in image processing. I wanted to challenge myself and not just...

C# Tutorial

Let’s Get Started A very basic problem that we face in image processing is how to scale images without making them look deformed when we want to make it of an arbitrary...

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.

1 2 | 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.

1 2 3 4 5 6 7 8 9 10 11 | 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.

1 2 | 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.

1 2 | int a = 8; double converted = (double)a; |

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

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

How to convert from string to a double

1 2 | string j = "5,6"; double k = Convert.ToDouble(j); |

Download Examples Next Chapter

## Comments