Exploring the Built-in Types of C#
In this guide, we will break into groups and explore each of the built-in data types in the C# programming language.
Dec 18, 2019 • 9 Minute Read
Introduction
In this guide, we will be exploring the built-in data types in the C# programming language. As of this writing, there are 15 different built-in types. We will break them into groups and explore each one of them.
By the end of this guide, you will know:
-
What are the built-in data types in C#
-
What each data type does and what they look like
-
When to use each data type given the scenario
The Built-in Data Types
I have broken down these data types into five different categories shown in the table below.
Integers
There are a number of different data types used to represent numbers. Each differs in their allowable range, so you should plan accordingly when deciding which one to use given your situation.
An int is a signed, 32-bit integer with an allowable range of -2,147,483,648 to 2,147,483,647. Because of its storage capacities and reasonable allowable range, the int is by far the most common integer type used. It is not as large as a long integer, but it is more maintainable in memory. It is not as maintainable in memory as a byte or a short, but it allows many more numbers to fit into it.
A uint is the same thing as an int, however, this is the unsigned version of it. This just means that the allowable range does not include negative numbers, so its range is 0 to 4,294,967,295, the same amount of numbers but in a different place on the scale. It has the same characteristics as an int other than that.
A byte and a sbyte are both 8-bit integers. The allowable range for a sbyte (known as a signed byte, meaning it allows negative numbers) is -128 to 127, and the allowable range for a byte is 0 to 255. Because of this small range, bytes and sbytes are not commonly used unless you know your number will remain in this range.
A short and a ushort are both 16-bit integers. The allowable range for a short is -32,768 to 32,767 and the allowable range for an unsigned short is 0 to 65,535. While this range is substantially larger than for bytes, shorts are also not as commonly used as int.
Lastly, there are long and ulong. As their names suggest, these keywords are reserved for very long numbers; they are 64-bit integers. The allowable range for a long is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, and the allowable range for an unsigned long is 0 to 18,446,744,073,709,551,615. Because of these enormous ranges, they require quite a bit of memory allocated to store. So while the range is very convenient for any programmer, the long and ulong data types are also not as frequently used as the int.
Floating Points
There are three different floating-point types of numbers. Each differs in their range, their performance, and their precision.
First we will look at float, which has the smallest range, the best performance, and a precision of seven digits, which is the worst out of the three. You would ideally use float when you need very fast results but do not really care about rounding errors.
A double has a substantially larger allowable range than a float, has good performance (but not as good as float), and has a precision of 15-16 digits, which is more than twice as good as float. It is the most commonly used floating-point type; so much so that by default, C# uses the double data type for all floating-type numbers. The double data type is ideal for any type of floating point arithmetic, hence its popularity. The only scenario where you should not use a double is for handling money and financial transactions.
That brings us to decimal. This data type is 100% able to accurately express any floating-point number within its allowable range, something that floats and doubles are not able to do. This is why the decimal type is widely used in financial transactions. This type of precision is necessary when handling large amounts of money and important financial transactions. Because of this high accuracy, the performance for a decimal type is slower than for a float or a double. The decimal data type has a very large range, slower performance, but pristine precision.
Below is an image that breaks down each float type by range and precision.
Objects
The built-in object type is simply an alias of the .NET framework Object, which all other classes are derived from. The object type has the ability to take the form of any other type and become said type through the processes of boxing and unboxing. (For more information on boxing and unboxing operations, you can check out one of my other guides, Recognize When Boxing and Unboxing Operations Occur, but we will avoid going in depth on that in this guide.)
Let's look at an example of an object taking the form of multiple other built-in types that we discuss in this guide. This is a major advantage of using an object type, and we will see this in the code block below. A disadvantage of using the object type is that it slows down the run-time of your program. So if you know what type your variable will be, you should use another type. But here is an example of the flexibility of the object type. Be sure to read the comments to see what is going on line by line.
object obj = "hello";
//declare object variable as a string
Console.WriteLine("Current value of obj: " + obj);
obj = 22;
//change it to an integer
Console.WriteLine("Current value of obj: " + obj);
obj = 'i';
//change it to a char
Console.WriteLine("Current value of obj: " + obj);
obj = "hello there";
//change it back to a string
Console.WriteLine("Current value of obj: " + obj);
The screenshot below shows the results of these four statements. All lines are compiled without any issues.
Chars
The two char data types are char and string. A char is a single Unicode literal character, but it can also represent a hexadecimal escape sequence, a Unicode representation, or even type cast integral codes which were previously represented by an integer. You represent a char in C# by surrounding it with single quotes (' '). A string is an array of chars. It is a series of chars strung together, usually meant to form words, sentences, etc. It is important to note that strings are immutable, meaning that once a string is created, it cannot be changed. (For more information on strings and immutability, check out my Understanding String Immutability in C# guide.)
Chars and strings can be explained best by showing them, as in this code block, where you can see a code example of each type of char discussed as well as a string.
char ch = 'x'; //Unicode literal character
char hexChar = '\xF'; //hexadecimal number
char unicodeChar = '\u0058'; //Unicode character
char space = (char)32; //type casted from an integer
string str = "hello world"; //example of a string
Logic
There is one data type that fits under the logic type, and that is the bool keyword. This keyword is used to represent two different Boolean values, true and false. This simply means that the value of this variable is either true or false. When you declare a bool variable, if you do not assign a value to it, the value will be defaulted to false. Below we see an example of the bool keyword in action. We iterate through a loop from 0 to 10 and check if each number is divisible by 2 using the % (mod) operator.
bool value = false; //declare a boolean with initial value of false
for (int i = 0; i < 11; i++)
{
if (i % 2 == 0)
{
value = true;
}
else
{
value = false;
}
Console.WriteLine(i + " % 2 = " + value);
}
Here is the corresponding output if you were to copy and paste this code into a terminal:
Conclusion
Built-in data types are the very foundation of the C# programming language. Each group of types is enormously important to learning programming. We touched on each of these data types, looked at examples, and learned about their limitations. It's their limitations that result in so many different options to choose from.
Expect to see many questions related to these data types if you are in a job interview. In particular, be prepared for questions about:
- The string class and several string manipulation procedures, such as reversing a string and finding out if a string is a palindrome.
- The object class and its uses of encapsulation, polymorphism, and inheritance.
- Boolean logic.
I hope you have enjoyed reading this guide and that it will help you understand one of the most important concepts of the computer science field! If you enjoyed this guide, please check out my other guides.