The Different Types of Data in C# - C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

C# Programming Bootcamp: Learn the Basics of C# Programming in 2 Weeks (2016)

Chapter 4. The Different Types of Data in C#

This chapter will discuss the various types of data you’ll encounter in C#. Study this chapter carefully. As a programmer, you’ll be dealing with different kinds of data in your codes and statements. That means you should be knowledgeable about these data types.

In the C# language, variables are divided into five types. Let’s discuss each type in detail:

The Value Type

You can directly assign values to this kind of variable. If you need to use them, just access a class named System.ValueType.

These variables hold data inside them. That means they don’t involve third-party tools or mechanisms. The list below will show you the value types available in C#:

· Int

· long

· char

· bool

· sbyte

· double

· decimal

· byte

· float

· ushort

· uint

· short

· ulong

The Reference Type

Variables that belong to this type don’t store the data directly. Instead, they point to the variables that hold the data. That means you should use this type if you want to point towards a file directory or memory location.

The Object Type

C# users consider this as the core class of all data in this language.“Object” serves as the alias for a C# class named System.Object. You can use object types to store other kinds of data. Before you can assign values, however, you have to perform type conversions.

The process of converting values into objects is known as boxing. Converting objects to values, on the other hand, is known as unboxing.

The Dynamic Type

This kind of variable allows you to store any value. With dynamic variables, type checking occurs during runtime. Here’s the syntax you need to use when declaring a dynamic data type:

dynamic <the variable’s name> = the value you want to assign;

For instance:

dynamic x = 99;

Dynamic data types and object data types are almost identical. The only difference is in the timing of their type checking. Object variables perform this task during compile time. Dynamic ones, on the other hand, perform type checking during runtime.

The String Type

You can use this type to assign string values to your chosen variable. This type serves as the alias for a C# class named System.String. According to computer experts, string variables are derived from the object type.

You can use string literals (i.e. quoted or @quoted) to assign the value for string type variables. Check the following example:

The @quoted string literal for that line is: