Table of contents
1.
Introduction
2.
Data Types in C#
2.1.
Value Data Types
2.2.
Reference Data Types
2.3.
Pointer Data Types
3.
Example of Data Types in C#
4.
FAQs
5.
Key Takeaways
Last Updated: Sep 11, 2024

Data Types in C#

Author Tanay kumar Deo
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C# is a strongly-typed programming language. It means that we should declare the data type of any variable that indicates the type and range of values it will store, such as float, integer, char, decimal, text, etc.

This article will focus on different types of Data types in C#, their default values, and ranges.

Recommended Topic, Palindrome in C# and Ienumerable vs Iqueryable.

Data Types in C#

Data types in C# specify the type of data that any variable can store, such as float, integer, char, etc. 

Data types in C# are mainly divided into three categories−

  • Value Data Types
  • Reference Data Types
  • Pointer Data Types

Value Data Types

In C#, the value data types will store the variable value in the memory. The value data types are floating-point-based and integer-based. C# supports both unsigned and signed literals. 

We have mainly two types of value Datatypes in C#:

  • Predefined Data Types - Char, Integer, Float, Boolean, etc.
  • User-defined Data Types - such as Enumerations, Structure, etc.

 

Now let's see some of the value data types in the below table:

Data types

Memory size

Range

char 1 byte -128 to 127
unsigned char 1 byte 0 to 127
signed char 1 byte -128 to 127
short 2 byte -32768 to 32767
unsigned short 2 byte 0 to 65535
signed short 2 byte -32768 to 32767
int 4 byte -2147483648 to 2147483647
unsigned int 4 byte 0 to 4294967295
signed int 4 byte -2147,483,648 to 2147,483,647
long 8 byte -9,223,372,036,854,775808 to 9,223,372,036,854,775807
unsigned long 8 byte 0 to 18,446,744,073,709,551615
signed long 8 byte -9,223,372,036,854,775808 to 9,223,372,036,854,775807
float 4 byte 1.5*10-45 to 3.4 *1038, with 7 digit precision
double 8 byte 5.0*10-324 - 1.7*10308, with 15 digit precision
decimal 16 byte at least -7.9*10?28 - 7.9*1028, with at least 28-digit precision

 

For example:

int myNum = 10;               // Integer (whole number)
double myDoubleNum = 10.99;  // Floating point number
char myLetter = 'C';         // Character
bool myBool = false;          // Boolean

 

The above code shows an example of value data types.

Reference Data Types

The reference data types do not contain any actual data stored in the variable, but they contain the variables' reference or memory address. If the data is changed by any of the reference data type variables, the other variable will automatically reflect the change in value.

We may categorize reference Data types in C# into two parts:

  • Predefined Types: These are built-in reference data types. For example, Objects and Strings.
  • User-defined Types: These are user-defined reference types. For example, Classes, Interface.

For example:

string myText = "CodingNinjas";     // String

 

The above code shows an example of predefined reference data types.

Pointer Data Types

Pointers in the C# programming language are variables; it is also known as an indicator or locator that points to the address of a value.

Pointer Data Type

In the above diagram, we can see that a pointer variable contains the address of any other variable.

Symbols used to represent pointers in data types in C#

We use different symbols or characters to represent pointer Datatypes in C#. These symbols are listed below in the table with their name and description.

Symbol Name Description
& ( ampersand sign ) Address operator We use it to determine the address of any variable.
* ( asterisk sign ) Indirection operator We use it to access the value of any address.

 

Declaring a pointer

We can declare a pointer in C# using the asterisk symbol ( * ). Let's see an example of declaring a pointer in C#.

int* p1, p;   // Valid syntax
int *p1, *p;   // Invalid

Example of Data Types in C#

In this section, we will see an example of creating and printing different types of Datatypes in C#.

using System;
namespace ValueTypeTest {
      
class CodingNinjas {
      
    // Main function
    static void Main()
    {
          
        // Declaring character
        char a = 'C';
 
        // Integer data type is used for numeric values
        int i = 10;
 
 
        // Long uses Integer values that may signed or unsigned
        long l = 456467;
 
        // Double type in C#
        double d = 8.358674532;
 
        // For float we use 'f' as suffix
        float f = 3.7330645f;
 
        // For float use 'm' as suffix
        decimal dec = 389.5m;
 
        Console.WriteLine("char: " + a);
        Console.WriteLine("integer: " + i);
        Console.WriteLine("long: " + l);
        Console.WriteLine("float: " + f);
        Console.WriteLine("double: " + d);
        Console.WriteLine("decimal: " + dec);
    }
}
}

 

The above code will produce an output as follows:

Output

Must Read IEnumerable vs IQueryable.

FAQs

  1. Is the memory size of all the value data types always fixed?
    The memory size of value data types is always fixed, but its value may differ in a 32bit and 64bit operating system.
     
  2. What is boxing in data types in C#?
    When a variable of value data type is converted to an object, it is known as boxing.
     
  3. What is unboxing in data types in C#?
    When a variable of type object is converted to a value data type, it is known as unboxing. 
     
  4. What is explicit conversion in data types in C#?
    Explicit conversion is done explicitly by the users using predefined functions. Explicit conversion requires a cast operator. 

    Syntax: 
    <type1> varible_name = (<type1>) varible_of_other_type.
     
  5. What is implicit conversion in data types in C#?
    The values of some data types are automatically converted to certain different C# Data Types. This is known as an implicit conversion. For example, converting an int data type to any float data type.
     
int i = 345;
float f = i;    

Key Takeaways

In this article, we have extensively discussed data types in C#, their definition, initialization, and naming conventions. We also learned about different types of data types in C#.

To study more about data types, refer to Abstract Data Types in C++.

Recommended Readings:

We hope that this blog has helped you enhance your knowledge regarding data types in C# and if you would like to learn more, check out our articles on Introduction To C#C# Comments, and Decision Making in C#. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass