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. |
|||
|
|||
17 comments :
Nice
Suresh Your c# articles are just awesome.
That was a helpful article!
Easy to understand articles.....Thank u suresh
good
good one...
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....
you are doing nice job
You are key for asp.net
Your site is very nice to understand and learning the new concepts
this question was asked in the interview,,,good one
Your all answers works perfectly
ok.. then convert.toString is there and it is safe, so what is the reason for giving method .ToString ???
Your Bolgs are very nice to understand and learning the new concepts
Note: Only a member of this blog may post a comment.