Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
C# .ToInt() Extension Method Example
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using Extensions; // Calling extension method .ToInt() will convert a string to Int or throw the exception "Input string was not in a correct format." // This works for internal variables and also Console.ReadLine(); namespace Rextester { public class Program { public static void Main(string[] args) { Console.WriteLine("32".ToInt() + "55".ToInt()); //Console.WriteLine(Console.ReadLine().ToInt() + Console.ReadLine().ToInt()); } } } namespace Extensions { public static class StringExtensions { public static int ToInt(this string s) { int returnInt = 0; try { returnInt = Int32.Parse(s); } catch(Exception ex) { throw ex; } return returnInt; } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,17 sec, absolute running time: 0,11 sec, cpu time: 0,11 sec, average memory usage: 15 Mb, average nr of threads: 3
edit mode
|
history
|
discussion
87