C# - Typecasting Of Int

  • Uploaded by: Zain Shaikh
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C# - Typecasting Of Int as PDF for free.

More details

  • Words: 214
  • Pages: 2
Int32.Parse, Convert.ToInt32, Int32.TryParse Comparing String to Integer Conversion Methods of .NET .Net provides several different ways to extract integers from strings. In this article, I will present the differences between Parse, TryParse and ConvertTo.

Parse: This function takes a string and tries to extract an integer from it and returns the integer. If the string is not a numerical value, the method throws FormatException. If the extracted number is too big, it throws OverflowException. Finally, if the string value is null, it throws ArgumentNullException. Int32 intValue = Int32.Parse(str);

Convert: This function checks for a null value and if the value is null, it returns 0 instead of throwing an exception. If the string is not a numerical value, the method throws FormatException. If the extracted number is too big, it throws OverflowException. Int32 intValue = Convert.ToInt32(str);

TryParse: This function is new in .Net 2.0. Since exception handling is very slow, TryParse function returns a boolean indicating if it was able to successfully parse a number instead of throwing an exception. Therefore, you have to pass into TryParse both the string to be parsed and an out parameter to fill in. Using the TryParse static method, you can avoid the exception and ambiguous result when the string is null.

bool isParsed = Int32.TryParse(str, out intValue);

Related Documents

Int
April 2020 38
Int
November 2019 46
Int
August 2019 52
Int
November 2019 57

More Documents from ""