Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Way to read input from console in C#
Use the ReadLine() method to read input from the console in C#. This method receives the input as string, therefore you need to convert it.
For example −
Let us see how to get user input from user and convert it to integer.
Firstly, read user input −
string val;
Console.Write("Enter integer: ");
val = Console.ReadLine();
Now convert it to integer −
int a = Convert.ToInt32(val);
Console.WriteLine("Your input: {0}",a);
Let us see this in an example. The input is added using command line argument−
Example
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string val;
Console.Write("Enter Integer: ");
val = Console.ReadLine();
int a = Convert.ToInt32(val);
Console.WriteLine("Your input: {0}",a);
}
}
The above will give the following result −
Enter Integer: 5 Your input: 5
Advertisements