C# Data Types with Example

Introduction :

The C# language comes with a set of Basic data types. These data types are used to build values which are used within an application. 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. so Before 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.

Syntax for defining Data Types in c#

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

In above syntax, we have added required data type before the variable name, compiler will check what datatype will hold the variable during the compilation.

  • [Data Type] :  it’s type of data , which hold the variable value such as int,float,double,string etc…
  • [Variable Name] :  it’s name of variable that holds value in application.
  • [value]:  it’s value of variable that we assign in application.

C# Data Types with Example

namespace Codelerner
{
    class Program
    {
        static void Main(string[] args)
        {
            int intnumber = 2;
            string strname = "C# Data Types with Example";
            char product = 'shopping';
            double Age = 20.23;
            bool isblTrue = true;

            Console.WriteLine(strname);
        }
    }
}

If you observe above, C# Data Types with Example, we have defined here multiple variables with different data types that’s we used in our application.

Data Types in C#

In c# programming language we have different type of data type that’s we have described below. there are mainly three data types are reference data type, value data type and pointer data type.

 Type  Data type
Reference data type string,class,delegate,object,interface etc..
Value data type int,bool,float,double,char,long,short etc…
Pointer data type Pointers.

Here we have represents pictorial diagram about different data types in the c# programming language.

C# Data Types with Example

C# Value Data Types

In C# programming language, Value data type will directly holds variable value in memory, Also it’s hold sign and unsigned both value in variable.

Data type .Net type Size Range
byte Byte 8 bits 0 to 255
sbyte SByte 8 bits -128 to 127
bool Boolean 8 bits True or False
short Int16 16 bits -32,768 to 32,767
ushort UInt16 16 bits 0 to 65,535
int Int32 32 bits -2,147,483,648 to 2,147,483,647
uint UInt32 32 bits 0 to 4294967295
float Single 32 bits -3.402823e38 to 3.402823e38
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
double Double 64 bits -1.79769313486232e308 to 1.79769313486232e308
decimal Decimal 128 bits (+ or -)1.0 x 10e-28 to 7.9 x 10e28

Reference Data Types

A reference type doesn’t store its value directly. Instead, it stores the address where the value is being stored. In other words, a reference type contains a pointer to another memory location that holds the data.

Following table describes list of reference type  that we have used in c# programming language.

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

Pointer Data Types

A pointer is a variable whose value is the address of another variable i.e., the direct address of the memory location. Similar to any variable or constant, you must declare a pointer before you can use it to store any variable address.

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# programming language.

Syntax of declaring the pointer type.

type* Product;

Example of defining the pointer.

int* x;
int* y;

The Below lists describes different type pointer symbols available in the c#.

Symbol Name Description
& Address Operator It’s useful to determine the address of a variable.
* Indirection Operator It’s useful to access the value of an address.

Leave a Reply

Your email address will not be published.