.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani

Home >> Interview Question >> C# >> Post New Question Subscribe to Interview Questions

What is the difference between convert.to.string and .to.string() method?

Posted By :Subhransu Sekhar Jena     Posted Date :October 30, 2014    Points :40   Category :C# 
Convert function handles nulls while i.string() does not it will throw a null reference exception error.

You can also find related Interview Question to What is the difference between convert.to.string and .to.string() method?  below: 

What is Difference Between ToString() vs Convert.ToString() vs (string) cast

There is a simple but important difference between these three.

ToString() raise exception when the object is null

So in the case of object.ToString(), if object is null, it raise NullReferenceException.

Convert.ToString() return string.Empty in case of null object

(string) cast assign the object in case of null

So in case of
MyObject o = (string)NullObject;

But when you use o to access any property, it will raise NullReferenceException.

Difference between String.Empty and ""

The main difference is lies on memory management.

String.Empty will not create any object of string where as "" (Double Quote or Empty Quote) will create a new object in the memory for the checking the objects. (More...)

How do I convert a numeric type to a string?

Call Sytem.Object.ToString(). For example, if the variable count is an integer, write the following:
string text = count.ToString(); (More...)

difference between string and StringBuilder?

String is immutable & StringBuilder is mutable.
Means we can add, remove or append the words in stringbuilder but not in string. And stringbuilder have larger range of storing than string. (More...)

What is the difference between Convert.toString and .toString()

Just to give an understanding of what the above question means see the below code :-

int val = 0;

We can convert the integer "val" using "val.ToString()" or "Convert.ToString" so what's the difference.

The basic difference between them is "Convert" function handles NULLS while "val.ToString()"
does not.

It will throw a NULL reference exception error. So as good coding practice using "Convert" is always safe. (More...)

What is the difference between Convert.ToString() and .ToString() method.

Have a look at the following program and its output

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExampleOfString
class Program
static void Main(string[] args)
String UserName = null ;
//Below statement WILL THROW an exception as .ToString() does not handles null values
String strUserId = UserName.ToString();
catch (NullReferenceException ex)
Console.WriteLine("Error:{0}", ex.Message);

//Below statement WILL NOT THROW an exception as convert.tostring handles implicitly
String strUserId = Convert.ToString(UserName);
Console.WriteLine("UserName:{0}", strUserId);
catch (NullReferenceException ex)
Console.WriteLine("Error:{0}", ex.Message);

Below is the output of the console application

Error:Object reference not set to an instance of an object.
Press any key to continue . . .
*/ (More...)

Difference between System.String and System.StringBuilder classes?

-- System.String is immutable; System.StringBuilder can have mutable string where a variety of operations can be performed. (More...)

What is the difference between string and stringbuilder?

Objects of type stringBuilder are mutable, whereas objects of string are immutable (More...)

What is the difference between String and StringBuilder?

When we use string object ot concatenate two strings , the first string is concatenate to the other string by creating a new copy in the memory as string object , and the old string is deleted , this process is very long so we say strings are immutable.SO we use string builder object where append method is used.
Stringbuilder is faster than string operation as copy is done at the same loication (More...)

How To Convert String to Integer in vb script or Numberformat.

Normally We Can Convert String To Integer using some Built-in Function.

2.Clng (More...)

Difference between String vs StringBuilder?

String Class
Once the string object is created, its length and content cannot be modified.
Even after object is created, it can be able to modify length and content.
Faster (More...)

Difference between String vs string?

1.String is a class(System.String)
2.String is a Reference type(class)

1.string is an alias name of String class that is created by microsoft
2.string is an value type(data type)
3.string is a C# keyword
4.string is a compiler shortcut for System.String class (More...)

Quick Links For Interview Questions Categories:
ASP.Net Windows Application   .NET Framework   C#   VB.Net   ADO.Net  
Sql Server   SharePoint   Silverlight   OOPs   JQuery   JavaScript/VBScript
Biztalk Patten/Practices .IIS WCF WPF WWF
Networking Aptitude Others   All      

Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.

Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.

So, Start looking our Interview Question section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend