using System;
public class MainClass {
public static void Main(string[] args) {
string sSentence = "";
for (; ; ) {
Console.WriteLine("Enter a string");
string sLine = Console.ReadLine();
if (IsTerminateString(sLine)) {
break;
}
sSentence = String.Concat(sSentence, sLine);
Console.WriteLine("
You've entered: {0}", sSentence);
}
}
public static bool IsTerminateString(string source) {
string[] sTerms = {"EXIT","exit","QUIT","quit"};
foreach (string sTerm in sTerms) {
if (String.Compare(source, sTerm) == 0) {
return true;
}
}
return false;
}
}
Month: February 2011
Convert input from control to upper case
using System;
public class MainClass {
static void Main() {
Console.WriteLine("Enter command:");
string resp = (Console.ReadLine()).ToLower();
switch (resp) {
case "a":
Console.WriteLine("Doing Task A");
break;
case "b":
Console.WriteLine("Doing Task B");
break;
case "c":
Console.WriteLine("Doing Task C");
break;
default:
Console.WriteLine("Bad choice");
break;
}
}
}
Use while(true) to read console input
using System;
public class MainClass {
static void Main() {
string resp;
while (true) {
Console.WriteLine("Menu
1 - Task A");
Console.WriteLine("2 - Task B");
Console.WriteLine("E - E(xit)");
resp = (Console.ReadLine()).ToLower();
if (resp == "e") {
break;
}
}
}
}
Use do while to read console input
using System;
public class MainClass {
static void Main() {
string resp;
do {
Console.WriteLine("Menu
1 - Task A");
Console.WriteLine("2 - Task B");
Console.WriteLine("E - E(xit)");
resp = (Console.ReadLine()).ToLower();
}
while (resp != "e");
}
}
Terminate a control input
using System;
public class MainClass {
static void Main() {
string resp;
Console.WriteLine("Enter command ('x' to end):");
while ((resp = (Console.ReadLine()).ToLower()) != "x") {
switch (resp) {
case "a":
Console.WriteLine("Doing Task A");
break;
case "b":
Console.WriteLine("Doing Task B");
break;
default:
Console.WriteLine("Bad choice");
break;
}
}
}
}
Output with parameters
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
public class AssignedValues {
static void Main( )
{
int myInt;
//other code here...
myInt = 7; // assign to it
System.Console.WriteLine("Assigned, myInt: {0}", myInt);
myInt = 5;
System.Console.WriteLine("Reassigned, myInt: {0}", myInt);
}
}
Read double and int from console
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
(new Program()).run();
}
public void run() {
double dailyRate = readDouble("Enter your daily rate: ");
int noOfDays = readInt("Enter the number of days: ");
writeFee(calculateFee(dailyRate, noOfDays));
}
private void writeFee(double p) {
Console.WriteLine("The consultant's fee is: {0}", p * 1.1);
}
private double calculateFee(double dailyRate, int noOfDays) {
return dailyRate * noOfDays;
}
private int readInt(string p) {
Console.Write(p);
string line = Console.ReadLine();
return int.Parse(line);
}
private double readDouble(string p) {
Console.Write(p);
string line = Console.ReadLine();
return double.Parse(line);
}
}

