Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Difference between Convert.tostring and .tostring() method Example

Oct 25, 2012
Introduction:

In this article I will explain differences between .tostring() and Convert.tostring() in asp.net.

Description:

In previous post I explained difference between web application and website, difference between datareader, dataadapter and dataset, difference between tinyint,smallint,int,bigint and many articles relating to asp.net, SQL Server. Now I will explain differences between .tostring() and Convert.tostring() in asp.net.

The basic difference between them is “Convert.ToString(variable)” handles NULL values even if variable value become null but “variable.ToString()” will not handle NULL values it will throw a NULL reference exception error. So as a good coding practice using “convert” is always safe.

Example

//Returns a null reference exception for str.
string str;
object i = null;
str = i.ToString();

//Returns an empty string for str and does not throw an exception. If you dont
string str;
object i = null;
str = Convert.ToString(i);
I hope it helps.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

16 comments :

SirigineediRavi said...

Nice

Asp DotNet said...

Suresh Your c# articles are just awesome.

hermes said...

That was a helpful article!

Menala Rajkumar said...

Easy to understand articles.....Thank u suresh

Menala Rajkumar said...
This comment has been removed by the author.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

good

Anonymous said...

good one...

ruchi jain said...

Ur site is enough to understand .net,sql,javascript etc...
and also enough to prepare for interview... once i went through ur site forgot to use other sites .... :) thanks a lot....

Anonymous said...

you are doing nice job

Anonymous said...

You are key for asp.net

Diviya Vinolin Add said...
This comment has been removed by the author.
Vikramraj Patil said...

Your site is very nice to understand and learning the new concepts

Sunil Gaded said...

this question was asked in the interview,,,good one

Anonymous said...

Your all answers works perfectly

Anonymous said...

ok.. then convert.toString is there and it is safe, so what is the reason for giving method .ToString ???

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.