C# Data Types with Examples

In c# programming language, Data Types are useful to define a type of data the variable can hold, such as integer, float, string, etc., in our application.

 

C# is a Strongly Typed programming language. Before we perform any operation on variables, it’s mandatory to define a variable with the required data type to indicate what type of data that variable can hold in our application.

Syntax of Defining C# Data Types

Following is the syntax of defining data types in the c# programming language.

 

[Data Type] [Variable Name];
[Data Type] [Variable Name] = [Value];

If you observe the above syntax, we added a required data type before the variable name to tell the compiler about what type of data the variable can hold or which data type the variable belongs to.

 

  • [Data Type] - It’s a type of data the variable can hold, such as integer, string, decimal, etc. 
  • [Variable Name] - It’s the name of the variable to hold the values in our application. 
  • [Value] - Assigning a required value to the variable.

Now we will see how to use the Data Types in our c# applications with examples.

C# Data Types Example

Following is the example of using the data types in the c# programming language.

 

namespace Tutlane
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 10;
            string name = "Suresh Dasari";
            double percentage = 10.23;
            char gender = 'M';
            bool isVerified = true;
       }
   }
}

If you observe the above c# data types example, we defined multiple variables with different data types based on our requirements.

Different Data Types in C#

In C# programming language, we have a 3 different type of data types, those are

 

TypeData Types
Value Data Type int, bool, char, double, float, etc.
Reference Data Type string, class, object, interface, delegate, etc.
Pointer Data Type Pointers.

The following diagram will illustrate more detail about different data types in the c# programming language.

 

Different Data Types in C# with Detailed Information

C# Value Data Types

In c#, the Value Data Types will directly store the variable value in memory. In c#, the value data types will accept both signed and unsigned literals.

 

The following table lists the value data types in c# programming language with memory size and range of values.

 

Data Type.NET TypeSizeRange
byte Byte 8 bits 0 to 255
sbyte SByte 8 bits -128 to 127
int Int32 32 bits -2,147,483,648 to 2,147,483,647
uint UInt32 32 bits 0 to 4294967295
short Int16 16 bits -32,768 to 32,767
ushort UInt16 16 bits 0 to 65,535
long Int64 64 bits -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong UInt64 64 bits 0 to 18,446,744,073,709,551,615
float Single 32 bits -3.402823e38 to 3.402823e38
double Double 64 bits -1.79769313486232e308 to 1.79769313486232e308
bool Boolean 8 bits True or False
decimal Decimal 128 bits (+ or -)1.0 x 10e-28 to 7.9 x 10e28
DateTime DateTime - 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999

C# Reference Data Types

In c#, the Reference Data Types will contain a memory address of variable value because the reference types won’t store the variable value directly in memory.

 

The following table lists the reference data types in c# programming language with memory size and range of values.

 

Data Type.NET TypeSizeRange
string String Variable Length 0 to 2 billion Unicode characters
object Object - -

C# Pointer Data Types

In c#, the Pointer Data Types will contain a memory address of the variable value. To get the pointer details we have two symbols ampersand (&) and asterisk (*) in c# language.

 

Following is the syntax of declaring the pointer type in the c# programming language.

 

type* test;

Following is the example of defining the pointer type in the c# programming language.

 

int* a;
int* b;

The following table lists the detail of different type pointer symbols available in the c# programming language.

 

SymbolNameDescription
& Address Operator It's useful to determine the address of a variable.
* Indirection Operator It's useful to access the value of an address.

This is how we can use the data types in c# applications based on our requirements.