INPUT STATEMENTS IN C#.NET – In this tutorial, you will learn how to pass input parameters to console application in c#, command line arguments in c#, how to take input in c# (runtime input in c#) with examples.
- C# supports three different types of input statements like java
Types
- Assignment inputs
- Command Line inputs
- Dynamic / Runtime inputs
I. ASSIGNMENT INPUTS (STATIC LEVEL INPUTS)
- This is 1st input method
- Fixed values
- This can be done by using the assignment operator (=)
I. EXAMPLE OF ASSIGNMENT INPUTS
1. SOURCE CODE
using p = System.Console; // p is alias here which is user defined
class Program
{
string name = “Ajith”;
int id = 191; // C# Type
// Int32 id = 191; // .NET Type
public void info()
{
p.WriteLine(“Name \t:”+name);
p.WriteLine(“Id \t:” + id);
}
static void Main()
{
p.WriteLine(“————————————“);
p.WriteLine(“\tAssignment Inputs”);
p.WriteLine(“————————————“);
// object creation like java style
Program obj = new Program();
// calling methods using object along with dot operator
obj.info();
}
}
2. OUTPUT
II. COMMAND LINE ARGUMENTS (CLA)
- Providing necessary input values (arguments) to the main() method at the time of program execution
- By default, all the command line parameters are string array (string[])
- An array index starts from 0 to n-1
- The pre-defined property Length returns the total number of command line arguments.
II. EXAMPLE OF COMMAND LINE ARGUMENTS
1. SOURCE CODE
using System;
using k = System.Console;
class Program
{
static void Main(string[] args)
{
k.WriteLine(“——————————-“);
k.WriteLine(“\tCommand Line Arguments”);
k.WriteLine(“——————————-“);
if (args.Length != 0)
{
k.WriteLine(“\tPLAYER NAMES”);
k.WriteLine(“Name [1]: \t”+args[0]);
k.WriteLine(“Name [2]: \t” + args[1]);
k.WriteLine(“Name [3]: \t” + args[2]);
k.WriteLine(“——————————-“);
}
else
k.WriteLine(“Zero Arguments!Plz Supply the Arguments”);
}
}
2. EDIT PROJECT PROPERTIES IN VISUAL STUDIO IDE
- Right click on the properties of current project in visual studio IDE
- Then click the “Debug” tab and add the necessary command line arguments separated by space.
3. OUTPUT
III. DYNAMIC INPUTS / INTERACTIVE INPUTS
- It is possible to give values to variables interactively through the keyboard at the time of execution (Providing values at the time of runtime)
- Dynamic inputs are done by using the predefined methods of the Console class in C#.NET
PREDEFINED METHODS
1. Read()
- Read a single character from keyboard
- It is an instance method of Console class
- It is similar to getc() method in c language
- Return Type: int
- Namespace: System
2. ReadLine()
- Read a line of text (string) from keyboard
- It is an instance method of Console class
- It is similar to gets() method in c language
- Return Type: string
3. ReadKey()
- Read a single character from keyboard
- Gets the character / function key pressed by the user. The pressed key is optionally displayed in the console window
- It is an instance method of Console class
- It is similar to getch() method in c language
- Return Type: ConsoleKeyInfo
4. Write()
- This is used to write / print the specified data to the standard output stream (standard output device)
- Here cursor does not move to next line
- It is an instance method of Console class
- Return Type: void
5. WriteLine()
- It writes / prints the specified string value to the standard output stream (Display the user message on the screen)
- Cursor moves to next line
- It is an instance method of Console class
- Return Type: void
- Namespace: System
II. EXAMPLE OF RUNTIME / DYNAMIC INPUTS
1. SOURCE CODE
using System;
using k = System.Console; // k is an alias here which can any user defined char or string.
class Program
{
string name, address;
int age;
public void Input()
{
k.Write(“Enter the Student Name : “);
name = k.ReadLine(); read a string (line of text)
k.Write(“Enter the Student Age : “);
string str = k.ReadLine(); read a string (line of text)
// convert string to int using parse()
age = int.Parse(str);
k.Write(“Enter the Student Place : “);
address = k.ReadLine(); read a string (line of text)
}
public void Output()
{
k.WriteLine(“\tStudent Profile”);
k.WriteLine(“—————————-“);
k.WriteLine(“Name\t:”+name);
k.WriteLine(“Age\t:” + age);
k.WriteLine(“Address\t:” + address);
k.WriteLine(“—————————-“);
}
static void Main(string[] args)
{
k.WriteLine(“—————————-“);
k.WriteLine(“\tDynamic or Runtime Inputs”);
k.WriteLine(“—————————-“);
// object creation like java
Program obj = new Program();
// calling instance methods using object like java
obj.Input();
obj.Output();
}
}
2. OUTPUT
More Tutorials
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…