/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
samples/snippets/csharp/programming-guide/string-to-number/parse-tryparse/Program.cs
61 строка
1 KB
David Pine
Fix the bug, print parsed number... (#22305)
13 янв 2021, 18:18
Не верифицирован
13 янв 2021, 18:18
179ce59
Код
Авторство
О чём код?
using System; public static class StringConversion { public static void Main() { string input = String.Empty; try { int result = Int32.Parse(input); Console.WriteLine(result); } catch (FormatException) { Console.WriteLine($"Unable to parse '{input}'"); } // Output: Unable to parse '' try { int numVal = Int32.Parse("-105"); Console.WriteLine(numVal); } catch (FormatException e) { Console.WriteLine(e.Message); } // Output: -105 if (Int32.TryParse("-105", out int j)) { Console.WriteLine(j); } else { Console.WriteLine("String could not be parsed."); } // Output: -105 try { int m = Int32.Parse("abc"); } catch (FormatException e) { Console.WriteLine(e.Message); } // Output: Input string was not in a correct format. const string inputString = "abc"; if (Int32.TryParse(inputString, out int numValue)) { Console.WriteLine(numValue); } else { Console.WriteLine($"Int32.TryParse could not parse '{inputString}' to an int."); } // Output: Int32.TryParse could not parse 'abc' to an int. } }