Exploring Variables, Data Types, and Operators

Simeon Vanov
4 min readApr 23, 2023

Variables, Data Types and Operators are the building blocks of all programming independant of the language that would be chosen. These building blocks are essential in order store, organize, and interact with the information our program works with.

  1. Variables — containers of data

For example, imagine we want to store the age of a person. We can create a variable called ‘age’ and ‘name’ and store the number representing the information in those:

string name = "Simeon"
int age = 29;

Now this variable is holding the data of 29 and it can be used throughout the code using the name ‘age’:

Console.WriteLine(age);

Why wouldn’t you just 29 directly in Console.WriteLine instead of using age and why are variable useful ? Well sure one can directly use 29 and not store it in a variable. But if you have many places in your code where you use the ‘age’ it will be useful to just have a single place which you need to change instead of many. Also in our current example the ‘age’ is known from the start of the program but in future we might not know exactly what number will be used because it will be using either from a User Input or from a Database. So we would need something to hold the input from the user and this holder is called a variable.

By assigning a unique name to each variable, we can easily refer to the data stored in it and use it in our calculations or operations. Think of variables as named placeholders for the values they hold.

2. Data Types: Categorizing Our Data

When we store information in variables, it’s crucial to know what kind of data we are dealing with. This is where data types come into play. You can see in the above code that the 2 variables hold different type of data. “Simeon” and 29 are essentially two different types as one is text and the other is number. Each programming languages have its own way to represent data types and basic types built-in in the language so that it is easier for the programmer to store data. In C#, the basic data types are:

int: Represents whole numbers (integers) without decimal points, such as 5, -3, or 42. Depending on the size of the number the data can be stored in a byte, short, int, long or ulong. But ‘int’ is the most common integer type to be used. You already so an example above:

int age= 29;

float: Represents single-precision floating-point numbers, which are decimal numbers with approximately 7 digits of precision. There is an ‘f’ at the end because by default the system will represent the number as ‘double’.

float heightInFeet = 5.80708661f;

double: Represents double-precision floating-point numbers, which are decimal numbers with approximately 15–17 digits of precision. Double as you can see holds the data with a higher precision.

double heightInFeet = 5.80708661;

char: Represents a single Unicode character, such as ‘a’, ‘A’, or ‘1’. Characters are enclosed in single quotes.

char firstLetterOfAlphabet = 'a'; 

bool: Represents a Boolean value, which can be either true or false. Booleans are used to represent yes/no or on/off situations in logic and decision-making.

bool isPerson = true;

string: Represents a sequence of characters, such as “hello” or “apple”. Strings are enclosed in double quotes.

string name = "Simeon";

3. Operators: Performing Actions on Our Data

Once we have our data stored in variables, we often need to perform various actions or calculations with it. Operators are the symbols or commands that allow us to do this. There is a result after the operation has finished and this result can be either stored in a new variable or an already existing variable can assigned the new value. There are several types of operators, in c#. Below is a list of them with examples storing the result of the operator:

a) Arithmetic operators:

  • Addition (+): int sum = 5 + 3;
  • Subtraction (-): int difference = 5 - 3;
  • Multiplication (*): int product = 5 * 3;
  • Division (/): int quotient = 5 / 3;
  • Modulus (%): int remainder = 5 % 3;

b) Comparison operators:

  • Equal to (==): bool isEqual = 5 == 3;
  • Not equal to (!=): bool isNotEqual = 5 != 3;
  • Greater than (>): bool isGreater = 5 > 3;
  • Less than (<): bool isLess = 5 < 3;
  • Greater than or equal to (>=): bool isGreaterOrEqual = 5 >= 3;
  • Less than or equal to (<=): bool isLessOrEqual = 5 <= 3;

c) Logical operators:

  • AND (&&): bool isTrue = (5 > 3) && (3 < 7);
  • OR (||): bool isEitherTrue = (5 > 3) || (3 > 7);
  • NOT (!): bool isFalse = !(5 > 3);

d) Assignment operators:

  • Assign (=): int a = 5;
  • Add and assign (+=): a += 3; // equivalent to a = a + 3;
  • Subtract and assign (-=): a -= 3; // equivalent to a = a - 3;
  • Multiply and assign (*=): a *= 3; // equivalent to a = a * 3;
  • Divide and assign (/=): a /= 3; // equivalent to a = a / 3;
  • Modulus and assign (%=): a %= 3; // equivalent to a = a % 3;

Next we will take look how to control the logic of the program using control structures (loops amd conditionals).

--

--

Simeon Vanov

Passionate developer with .NET & cloud expertise. Sharing knowledge on SOLID, testing, design patterns, web concepts & microservices. Let's learn together!