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); } }
illustrates how to read command-line arguments
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example10_4.cs illustrates how to read
command-line arguments
*/
using System;
public class Example10_4
{
public static void Main(string[] arguments)
{
for (int counter = 0; counter < arguments.Length; counter++) { Console.WriteLine("arguments[" + counter + "] = " + arguments[counter]); } } } [/csharp]
Program argument input values: Converts degrees Celsius to degrees Fahrenheit
using System; public class PassArguments { public static void Main(String[] args) { double hotC, coldC; double hotF, coldF; if (args.Length != 2) { Console.WriteLine ("Enter a hot and a cold temerature as program arguments."); return; } hotC = double.Parse(args[0]); hotF = 9.0*hotC/5.0 + 32.0; Console.WriteLine ("The Fahrenheit temperature is: {0:F1}", hotF); coldC = double.Parse(args[1]); coldF = 9.0*coldC/5.0 + 32.0; Console.WriteLine ("The Fahrenheit temperature is: {0:F1}", coldF); } }
To Four Digit Year
using System; using System.Globalization; class Class1 { static void Main(string[] args) { Calendar MyCalendar = new GregorianCalendar(); Console.WriteLine(MyCalendar.ToFourDigitYear(32)); Console.WriteLine(MyCalendar.ToFourDigitYear(28)); } }
Two Digit Year Max
using System; using System.Globalization; class Class1 { static void Main(string[] args) { Calendar MyCalendar = new GregorianCalendar(); CultureInfo MyCulture = new CultureInfo("es-ES"); DateTime MyDate = new DateTime(2006,8,22,15,30,0,0); Console.WriteLine(MyCalendar.TwoDigitYearMax); } }