What are different .NET Data Types, Difference between string and String and Difference between String and String Builder

 .Net Data Types-

. Net Data Types can be classified in 3 categories –

1. Any data- byte, char, string

2. Numeric data-

I) with decimal- float, double, decimal

II) without decimal-

i)           unsigned- ushort, uint, ulong

ii)          signed- short, int, long

3.  Other- bool, datetime

1. Any data-

byte- byte is unsigned 8 bit length data type. So it has minimum value=0 and maximum value=255.

char- char is unsigned 2 byte length data type which can contain unicode data. So it has minimum value=0 and maximum value=65535.

string- string is a series of char data.

2. Numeric data-

     I) with decimal-

     float- 7 precision (after . we can put 7 numbers)

     double- 16 precision (after . we can put 16 numbers)

     decimal- 29 precision(after . we can put 29 numbers)

     II) without decimal-

           i)unsigned-

           ushort(uInt16)- uInt16 is a 16 bit unsigned numeric variable. So it has minimum value=0 and maximum value=65535.

           uint(uInt32)- uInt32 is a 32 bit unsigned numeric variable. So it has minimum value=0 and maximum value=2^32.

           ulong(uInt64)- uInt64 is a 64 bit unsigned numeric variable. So it has minimum value=0 and maximum value=2^64

           ii) signed-

           short(Int16)- Int16 is a 16 bit signed numeric variable. So it has minimum value=-32767 and maximum value=+32767.

           int(Int32)- Int32 is a 32 bit signed numeric variable. So it has minimum value=-2147483646 and maximum value=+2147483646.

           long(Int64)- Int64 is a 64 bit signed numeric variable. So it has minimum value=-2^32 and maximum value=2^32

3. Other-

bool- it will store only true and false value.

datetime- it will store date and time only.

 

Selection of appropriate data type is most important for performance aspect.

 

Difference between String and string?

There is no difference between String and string. string is just an alias on System.String. There are lots of aliases like we have String and string-

object: System.Object

string: System.String

bool: System.Boolean

byte: System.Byte

sbyte: System.SByte

short: System.Int16

ushort: System.UInt16

int: System.Int32

uint: System.UInt32

long: System.Int64

ulong: System.UInt64

float: System.Single

double: System.Double

decimal: System.Decimal

char: System.Char

 

Difference between String and String Builder?

Strings are immutable. It means that the value will not change. So rather than changing the value it will create new copies of data and storing the current values in new copies.

String x=”Akash”;

x=”Amit”;

it will create 2nd copy and x will point to the latest copy.

We can prove this with the help of following example-

string x=””;

for(int 1=0;i<10000;i++)

{

x=”Akash”+x;

}

After build this application if we see the performance of this application in CLRProfiler then we can see that approx 96% of memory allocated by string variable out of whole memory allocated to run the application this shows that string has multiple copies.

 StringBuilder is mutable. It means that the value will change, and it will not create multiple copies.

 StringBuilder x=new StringBuilder();

for(int 1=0;i<10000;i++)

{

x.Append(“Akash”);

}

After build this application if we see the performance of this application in CLRProfiler then we can see that approx 60% of memory allocated by string variable out of whole memory.

So the conclusion is use StringBuilder when you think your value will change again and again and again. In the other end Use string when you think your value will change very less.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s