
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to read inputs as integers in C#?
To read inputs as integers in C#, use the Convert.ToInt32() method.
res = Convert.ToInt32(val);
Let us see how −
The Convert.ToInt32 converts the specified string representation of a number to an equivalent 32-bit signed integer.
Firstly, read the console input −
string val; val = Console.ReadLine();
After reading, convert it to an integer.
int res; res = Convert.ToInt32(val);
Let us see an example −
Example
using System; using System.Collections.Generic; class Demo { static void Main() { string val; int res; Console.WriteLine("Input from user: "); val = Console.ReadLine(); // convert to integer res = Convert.ToInt32(val); // display the line Console.WriteLine("Input = {0}", res); } }
Output
Input from user: Input = 0
The following is the output. The input is entered by the user.
Input from user: 2 Input = 2
- Related Articles
- How can we read inputs as integers in Python?
- How to read inputs as strings in C#?
- How to read integers from a file using BufferedReader in Java?
- Read integers from console in Java
- Read integers from a text file with C++ ifstream
- Count ways to express ‘n’ as sum of odd integers in C++
- How to add read-only property in C#?
- How to create a function in R with two inputs?
- Ways to write N as sum of two or more positive integers in C++
- How to read and parse CSV files in C++?
- How to read file content into istringstream in C++?
- Count ‘d’ digit positive integers with 0 as a digit in C++
- How to sum two integers without using arithmetic operators in C/C++?
- How to force Matplotlib to show the values on X-axis as integers?
- Using return value of cin to take unknown number of inputs in C++

Advertisements