Introduction:

In this article we will explain what are the data types in c# . Different between value type and reference type or how to define a value type variable and reference type variable with example:

Description:

C# is a strongly typed language. You cannot declare a variable without data types. C# provides two types of data types: Value types and Reference types.

 

Value type:

In c# Value type variables can be assigned a value directly.  Value Type variables are stored in the stack. It is derived from the class System.ValueType.

 The following lists the available value types

Data Type

Width

Value Range

byte

8 bit

0 to 255

sbyte

8 bit

-128 to 127

int

32 bit

-2,147,483,648 to 2,147,483,647

uint

32 bit

0 to 4294967295

short

16 bit

-32,768 to 32,767

ushort

16 bit

0 to 65535

long

64 bit

-9223372036854775808 to 9223372036854775807

ulong

64 bit

0 to 18446744073709551615

float

32 bit

-3.402823e38 to 3.402823e38

double

64 bit

-1.79769313486232e308 to 1.79769313486232e308

char

16 bit

Unicode symbols used in text

bool

8 bit

True or false

decimal

128 bit

±1.0 × 10e−28 to ±7.9 × 10e28

For Example Value Type :

int i = 123;


Reference Type:

The reference data type does not store the actual data in a variable, but it is store a reference to the variables. Reference Type variables are stored in the heap , the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value.

For example reference types: objectdynamic, and string.

dynamic a = 100; 
string b = "aditya";
object c = 500;

Thanks.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like