Data Types in C#

Data Types in C#

In this article, I am going to discuss the Data Types in C#. As a developer, it is very important to understand the Data Type in C#. At the end of this article, you will be in a better position to decide which data type you need to use for a specific type of values with examples. Please read our previous article where we discuss the Console class Methods and Properties in C# before proceeding to this article.

Why we need data types in C#?

The Datatypes in C# are basically used to store the data temporarily in the computer through a program. In the real world, we have different types of data like integer, floating-point, character, string, etc. To store all these different kinds of data in a program to perform business-related operations, we need the data types.

What is a data type in C#?

The Datatypes are something which gives information about

  1. Size of the memory location.
  2. The range of data that can be stored inside that memory location
  3. Possible legal operations which can be performed on that memory location.
  4. What types of results come out from an expression when these types are used inside that expression.

The keyword which gives all the above information is called the data type.

Data type classifications in C#:

A data type in C# specifies the type of data that a variable can store such as integer, floating, character, string, etc. The following diagram shows the different types of data Types available in C#.

Data Types in C#

There are 3 types of data types available in the C# language.

  1. Value Data Type
  2. Reference Data Type
  3. Pointer Data Type

Let us discuss each of these data types in detail

Value Data Type in C#

The data types which stores the value directly called as the value Data Type. They are derived from the class System.ValueType. The examples are int, char, and float which stores numbers, alphabets, and floating point numbers respectively. The value data types in C# again classified into two types are as follows.

  1. Predefined Data Types – Example includes Integer, Boolean, Float, etc.
  2. User-defined Data Types – Example includes Structure, Enumerations, etc.

Based on the Operating system (32 or 64-bit), the size of the memory of the data types may change.  If you want to know the actual size of a type or a variable on a particular operating system, then you can make use of the sizeof method. 

Let’s understand this with an example. The following example gets the size of int type on any platform.

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Size of int: {0}", sizeof(int));
            Console.ReadKey();
        }
    }
}

When we execute the above code, it gives the following output:

Data Types in Csharp

Reference Data Type in C#

The data type which is used to store the reference of a variable is called as Reference Data Types. In other words, we can say that the reference types do not store the actual data stored in a variable, rather they store the reference to the variables. We will discuss this concept in a later article.

Again, the Reference Data Types are categorized into 2 types. They are as follows.

  1. Predefined Types – Examples include Objects, String and dynamic.
  2. User-defined Types – Examples include Classes, Interface.

Pointer Type

The pointer in C# language is a variable, it is also known as locator or indicator that points to an address of a value that means pointer type variables stores the memory address of another type. We will discuss this concept in detail in a later article.

Built-in Data Types in C#

The built-in Data Types in C# are as follows

  1. Boolean type – Only true or false 
  2. Integral Types – sbyte, byte, short, ushort, int, uint, long, ulong, char
  3. Floating Types – float and double
  4. Decimal Types
  5. String Type 
Escape Sequences in C#:

Verbatim Literal is a string with an @ symbol prefix, as in @“Hello”.  The Verbatim literals make escape sequences translate as normal printable characters to enhance readability. 

Without Verbatim Literal: “C:\\Pranaya\\DotNetTutorials\\Csharp” – Less Readable

With Verbatim Literal: @“C:\Pranaya\ DotNetTutorials\Csharp” – Better Readable

Let’s understand this with an example:

namespace FirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            // Displaying double quotes in c#
            string Name = "\"Dotnettutorials\"";
            Console.WriteLine(Name);

            // Displaying new line character in c#
            Name = "One\nTwo\nThree";
            Console.WriteLine(Name);

            // Displaying new line character in c#
            Name = "c:\Pranaya\Dotnettutorials\Csharp";
            Console.WriteLine(Name);

            // C# verbatim literal
            Name = @"c:\Pranaya\Dotnettutorials\Csharp";
            Console.WriteLine(Name);
            Console.ReadKey();
        }
    }
}

OUTPUT:

Data Types in Csharp

In the next article, I will discuss the Static and Non-Static Member in C#.

SUMMARY:

In this article, I try to explain the Data Types in C#. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *