Author avatar

Dániel Szabó

Comparison Operators and Built-in Types in C#

Dániel Szabó

  • Mar 19, 2020
  • 7 Min read
  • 561 Views
  • Mar 19, 2020
  • 7 Min read
  • 561 Views
Languages
Frameworks
and Tools
Programming Languages
Hosted
C#

Introduction

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 Types

Built-in ypes fall into five different categories.

  1. Integers (int, uint, long, etc..)
  2. Floating point (decimal, double, float)
  3. Object (object)
  4. Char (string, char)
  5. Logical (bool)

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;

namespace Pluralsight
{

    public class BuiltInSize
    {

        public static void Main()
        {                            
            Console.WriteLine($"int has size of {sizeof(int)} bytes");
            Console.WriteLine($"double has size of {sizeof(double)} bytes");
            Console.WriteLine($"decimal has size of {sizeof(decimal)} bytes");
            Console.WriteLine($"char has size of {sizeof(char)} bytes");                
            Console.WriteLine($"bool has size of {sizeof(bool)} bytes");
            Console.ReadKey();            
        }
    }
}
csharp

The output should be as follows.

1
2
3
4
5
int has size of 4 bytes
double has size of 8 bytes
decimal has size of 16 bytes
char has size of 2 bytes
bool has size of 1 bytes
bash

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.

  1. The storage space needed to be allocated in memory for the type
  2. Maximum and minimum values, except for strings and custom objects
  3. Any member declarations
  4. The base type our type inherits from
  5. The location where the memory for variables will be allocated at runtime (stack or heap)
  6. The permitted operations

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

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.

  1. Arithmetic
  2. Relational
  3. Logical
  4. Bitwise
  5. Assignment
  6. Conditional

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.

Less-than Operator <

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.

1
2
3
Console.WriteLine($"5 < 7 : {5 < 7}");
Console.WriteLine($"3.0 < 11.99 : {3.0 < 11.0}");
Console.WriteLine($"a < b {'a' < 'b'}");
csharp

The output should be as follows.

1
2
3
5 < 7 : True
3.0 < 11.99 : True
a < b True
bash

The following comparisons would result in a compile time error.

1
2
Console.WriteLine("asting" < 'b');
Console.WriteLine(true < false);
csharp

The reason for this is that the operator does not support string and bool operands.

Greater-than Operator >

This operator will return true if the left-hand operand is greater than the right hand operand, otherwise false.

1
2
3
Console.WriteLine($"5 > 7 : {5 > 7}");
Console.WriteLine($"3.0 > 11.99 : {3.0 > 11.0}");
Console.WriteLine($"a > b {'a' > 'b'}");
csharp

The output should be as follows.

1
2
3
5 > 7 : False
3.0 > 11.99 : False
a > b False
bash

Less-than-or-equal Operator <=

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.

1
2
3
Console.WriteLine($"5 <= 7 : {5 <= 7}");
Console.WriteLine($"3.0 <= 11.99 : {3.0 <= 11.0}");
Console.WriteLine($"a <= b {'a' <= 'b'}");
csharp

The output should be as follows.

1
2
3
5 <= 7 : True
3.0 <= 11.99 : True
a <= b True
bash

Greater-than-or-equal Operator >=

This operator will return true if the left-hand operand is greater than or equal to the right hand operand, otherwise false.

1
2
3
Console.WriteLine($"5 >= 7 : {5 >= 7}");
Console.WriteLine($"3.0 >= 11.99 : {3.0 >= 11.0}");
Console.WriteLine($"a >= b {'a' >= 'b'}");
csharp

The output should be as follows.

1
2
3
5 >= 7 : False
3.0 >= 11.99 : False
a >= b False
bash

Equal-to Operator ==

This operator will return true if the left hand operand is equal to the right hand operand, otherwise false.

1
2
3
Console.WriteLine($"5 == 7 : {5 == 7}");
Console.WriteLine($"3.0 == 3.00 : {3.0 == 3.00}");
Console.WriteLine($"c == c {'c' == 'c'}"); 
csharp

The output should be as follows.

1
2
3
5 == 7 : False
3.0 == 3.00 : True
c == c True
bash

Not-equal-to Operator ==

This operator will return true if the left hand operand is not equal to the right hand operand, otherwise true.

1
2
3
Console.WriteLine($"5 != 7 : {5 != 7}");
Console.WriteLine($"3.0 != 3.00 : {3.0 != 3.00}");
Console.WriteLine($"c != c {'c' != 'c'}");
csharp

The output should be as follows.

1
2
3
5 != 7 : True
3.0 != 3.00 : False
c != c False
bash

Conclusion

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!

5