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


Top 5 Contributors of the Month
david stephan

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

Difference between Int32.Parse,Convert.Int32

Posted By :Syed Shakeer Hussain     Posted Date :June 28, 2009    Points :10   Category :C# 
Int32.Parse Method:
-------------------
Converts the string representation of a number to its 32-bit signed integer equivalent.
-When s is a null reference, it will throw ArgumentNullException.
-If s is other than integer value, it will throw FormatException.
-When s represents a number out of range, it will throw OverflowException.

Convert.ToInt32(string):
-------------------------
Converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse () method.
-When s is a null reference, it will return 0 rather than throw ArgumentNullException.
-If s is other than integer value, it will throw FormatException.
-When s represents a number out of range, it will throw OverflowException.

Int32.TryParse Method:
----------------------
Converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise.
-When s is a null reference, it will return 0.
-If s is other than an integer value, the out variable will have 0.
-When s represents a number out of range, the out variable will have 0.

You can also find related Interview Question to Difference between Int32.Parse,Convert.Int32  below: 

What is the difference between int.Parse and int.TryParse methods?

  
Parse method throws an exception if the string you are trying to parse is not a valid number where as TryParse returns false and does not throw an exception if parsing fails. Hence TryParse is more efficient than Parse (More...)

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.
(More...)

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

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

int val = 0;
MessageBox.Show(val.ToString());
MessageBox.Show(Convert.ToString(val));


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 ;
try
{
//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);
}

try
{
//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.
UserName:
Press any key to continue . . .
*/ (More...)

What is the Difference between Convert.ToString() and .ToString()

  
Generally Convert.ToString() handles NULLs where as .ToString will return a Null Reference Exception.

So it would be good if we use Convert.ToString() all the time for casting purposes. (More...)

Difference between convert.Tostring() and .tostring()?

  
Convert.Tostring()
-- Support Null Value
.ToString()
-- Not support null value (More...)

Difference between int.Parse and int.TryParse

  
Parse method throws an exception, if the string you are trying to parse is not a valid number , whereas TryParse returns false and does not throw an exception if parsing fails. (More...)

Which one is faster?
int.Parse()
int.TryParse()
Convert.ToInt32()

  
int.TryParse() is relativly faster when compared to others (More...)

Difference between Int.parse and int.TryParse?

  
Int.parse: Convert string to integer.it thrown exception when null or invalid input is provided with the Parse menthod
Int.TryParse: Does not thrown exception instead it returns boolean value.
(More...)

Difference between Double.Parse and Double.TryParse ?

  
Double.Parse will throw an exception on non-numbers or null
Double.TryParse will return false or 0 on any of the above without generating an exception. (More...)

Difference between Parse vs TryParse?

  
Parse method is used to parse any value to specified data type.
TryParse is a good method if the string you are converting to an integer is not always numeric. The TryParse method returns a boolean to denote whether the conversion has been successfull or not, and returns the converted value through an out parameter. (More...)

What is difference between convert.tostring() and tostring()?

  
The main difference between ToString() and Convert.ToString() is,"Convert" function handles NULLS while "variable.ToString()" does not it will throw a NULL reference exception error. So as a good coding practice using "convert" is always safe.

Session["test_variable"] ="test";
Response.Write(Convert.ToString(Session["test_variable"])); //it will display "test"
Response.Write(Session["test_variable"].ToString());//it will display "test"

//ERROR will not come in above statement because declared sesssion contains a value

Session["test_variable"] = null;
Response.Write(Convert.ToString(Session["test_variable"])); //it will convert to blank value ,No erro will come
Response.Write(Session["test_variable"].ToString());//will not handle the NULL values and it throws a NULL Reference Exception e
(More...)

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

  
Convert function handles nulls while i.string() does not it will throw a null reference exception error. (More...)

Difference Between Delete and Truncate

  
.Delete table is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow.

. Truncate table also deletes all the rows in a table, but it won't log the deletion of each row, instead it logs the de-allocation of the data pages of the table, which makes it faster. Of course, truncate table cannot be rolled back.

. Truncate table is functionally identical to delete statement with no "where clause" both remove all rows in the table. But truncate table is faster and uses fewer system and transaction log resources than delete.

. Truncate table removes all rows from a table, but the table structure and its columns, constraints, indexes etc., remains as it is.

. In truncate table the counter used by an identity column for new rows is reset to the seed for the column.

. If you want to retain the identity counter, use delete statement instead.

. If you want to remove table definition and its data, use the drop table statement.

. You cannot use truncate table on a table referenced by a foreign key constraint; instead, use delete statement without a where clause. Because truncate table is not logged, it cannot activate a trigger.

. Truncate table may not be used on tables participating in an indexed view. (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