Operators are part of every programming language, and C# is no different. When it comes to business logic inside our applications, there are two main types to consider. One is built-in, and the other is custom. In this guide, we will cover built-in types and what types of operators are provided by C#. After this, we will dive deeper into comparison operators.
Built-in ypes fall into five different categories.
These could be expanded into further subcategories, but the most important thing to remember is that they are different in resolution and the amount of space allocated in memory for storing them. There is a neat trick I would like to show you that demonstrates how to allocate the size of a specific instance in memory.
1using System;
2
3namespace Pluralsight
4{
5
6 public class BuiltInSize
7 {
8
9 public static void Main()
10 {
11 Console.WriteLine($"int has size of {sizeof(int)} bytes");
12 Console.WriteLine($"double has size of {sizeof(double)} bytes");
13 Console.WriteLine($"decimal has size of {sizeof(decimal)} bytes");
14 Console.WriteLine($"char has size of {sizeof(char)} bytes");
15 Console.WriteLine($"bool has size of {sizeof(bool)} bytes");
16 Console.ReadKey();
17 }
18 }
19}
The output should be as follows.
1int has size of 4 bytes
2double has size of 8 bytes
3decimal has size of 16 bytes
4char has size of 2 bytes
5bool has size of 1 bytes
When you are developing any application, you should always use the type closest in allowed size to what your data may become. This reduces the memory footprint and results in a better performing app that is easy to troubleshoot.
The object type is nothing more than an alias for the built-in type of the .NET framework object. Its speciality is that every new object inherits from it and can further expand on the capabilities inherited.
There are some limitations to built-in types as to what kind of information can be stored in them.
C# was written with common sense, so permitted operations on specific types should come naturally after a while. For example, there would be no point in dividing a string with a float or multiplying the true value of a bool variable with a string.
Operators are basically special symbols that take an operand or two and perform specific actions based on the permitted operations of the given type. There are six main categories for these operators on built-in types.
Some operators can be overloaded. The point of overloading them is to imbue their capabilities to work with user-defined types.
When you have an expression, operator precedence and associativity will determine the order of the operations and how they are performed. The jolly joker is the parenthesis, which can override default behavior.
Comparison operators fall into the relational operators category and are supported by all integral and floating point numeric types.
This operator will return true if the left-hand operand is less than the right-hand operand, otherwise it will be false.
Let's look at a small demonstration.
1Console.WriteLine($"5 < 7 : {5 < 7}");
2Console.WriteLine($"3.0 < 11.99 : {3.0 < 11.0}");
3Console.WriteLine($"a < b {'a' < 'b'}");
The output should be as follows.
15 < 7 : True
23.0 < 11.99 : True
3a < b True
The following comparisons would result in a compile time error.
1Console.WriteLine("asting" < 'b');
2Console.WriteLine(true < false);
The reason for this is that the operator does not support string and bool operands.
This operator will return true if the left-hand operand is greater than the right hand operand, otherwise false.
1Console.WriteLine($"5 > 7 : {5 > 7}");
2Console.WriteLine($"3.0 > 11.99 : {3.0 > 11.0}");
3Console.WriteLine($"a > b {'a' > 'b'}");
The output should be as follows.
15 > 7 : False
23.0 > 11.99 : False
3a > b False
The operator will return true if the left-hand operand is less than or equal to the right-hand operand, otherwise it will be false.
1Console.WriteLine($"5 <= 7 : {5 <= 7}");
2Console.WriteLine($"3.0 <= 11.99 : {3.0 <= 11.0}");
3Console.WriteLine($"a <= b {'a' <= 'b'}");
The output should be as follows.
15 <= 7 : True
23.0 <= 11.99 : True
3a <= b True
This operator will return true if the left-hand operand is greater than or equal to the right hand operand, otherwise false.
1Console.WriteLine($"5 >= 7 : {5 >= 7}");
2Console.WriteLine($"3.0 >= 11.99 : {3.0 >= 11.0}");
3Console.WriteLine($"a >= b {'a' >= 'b'}");
The output should be as follows.
15 >= 7 : False
23.0 >= 11.99 : False
3a >= b False
This operator will return true if the left hand operand is equal to the right hand operand, otherwise false.
1Console.WriteLine($"5 == 7 : {5 == 7}");
2Console.WriteLine($"3.0 == 3.00 : {3.0 == 3.00}");
3Console.WriteLine($"c == c {'c' == 'c'}");
The output should be as follows.
15 == 7 : False
23.0 == 3.00 : True
3c == c True
This operator will return true if the left hand operand is not equal to the right hand operand, otherwise true.
1Console.WriteLine($"5 != 7 : {5 != 7}");
2Console.WriteLine($"3.0 != 3.00 : {3.0 != 3.00}");
3Console.WriteLine($"c != c {'c' != 'c'}");
The output should be as follows.
15 != 7 : True
23.0 != 3.00 : False
3c != c False
In this guide, you have learned what operators are and what the most important built-in types are. We took the relational operators and demonstrated all of their functionality on three basic data types. I hope this has been informative to you and I would like to thank you for reading it!