C# Primitive Types - Part 1- char and bool

C# Primitive Types - Part 1- char and bool

Hi!

Today, I am going to introduce you to or remind you of primitive types. As a .NET developer who has "some" extent of knowledge about C# language, I can say that this is a fundamental topic to cover when learning programming in our beloved language.

If you don't know what a variable, stack or heap are, I invite you to my other posts about the basics of C#, data structures and algorithms.

The primitive types, also called value types or simple types, are predefined in the C# language. They can be assigned a value directly, which is stored on the stack, not on the heap.

Char (Character) - Unicode symbols used in text

It represents UTF-16 single character.

You can define a char value using:

  • a character literal ('K')

  • a Unicode escape sequence (\u004B)

  • a hexadecimal escape sequence (\x004B)

  • casting ( (char)75)

You can find all of the Unicode characters on this list.

As we all know C# is an object-oriented language so the keyword char is an alias for System.Char structure type (I will write about structures later).

The char type can be implicitly converted into other integral types of C# language: ushort, int, uint, long and ulong. You can implicitly convert chars into floating-point numeric types such as float, double and decimal or explicitly into sbyte, byte and short.

It supports such operations as comparison, equality, increment and decrement operators. Arithmetic and bitwise logical operators are working on char code and are producing results of the int type.

Fun fact

When I was a student we were writing software in C++ for microcontrollers. I remember when I imported a library containing an implementation of string into my project, the professor went mad at me. He was super crazy about not using a table of chars which is almost THE SAME THING BY THE WAY! But yes... A string is a reference type which is an array of chars (char[]).

Bool (Boolean)

It represents a logical state (true or false). Mainly used to control algorithm flow and decide whether to use some code block or not.

What's important to mention is Boolean logical operators that provide us with the possibility to create and combine multiple conditional expressions in if, do, while and for statements.

What's cool and kind of fresh is using booleans as feature flags. Feature flags (I'll probably do another post about FF in the future) are configurable switches that toggle some features of our app. For example, we can deploy part of a new feature and not publish it for users. After testing is completed we just switch a flag from false to true in our configuration and voilà we just delivered a new toy to our customers.

It's not so primitive huh?

To be continued...

To prevent making this post taking more than 5 minutes to read I am cutting it here and writing about integral numeric types and floating-point numeric types later.

I hope you enjoyed this short reminder ;)