HAVIT Knowledge Base

Vývoj webových aplikací, .NET, SQL, návrh
Welcome to HAVIT Knowledge Base Sign in | Join | Help
-
Home Články Forums Obrázky Soubory

.NET Framework

Microsoft .NET Framework, Base Class Library

Rozdíl mezi int.Parse() a Convert.ToInt32()

Mnoho vývojářů tápe, kterou z metod Int32.Parse() a Convert.ToInt32() používat pro parsování textových vstupů na číslo. Rozdíl je malý, a sice v interpretaci vstupu null. Vše je jasné, pokud se podíváme na vnitřnosti Convert.ToInt32():
public static int ToInt32(string value)
{
   if (value == null)
   {
      return 0;
   }
   return int.Parse(value, CultureInfo.CurrentCulture);
}
Metoda Convert.ToInt32() tedy na vstup null vrátí hodnotu 0, zatímco int.Parse() vyhodí výjimku ArgumentNullException.
Published 30. dubna 2006 23:49 by Robert Haken
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

What do you think?

(required) 
(optional)
(required) 
Enter the code you see below

Submit