Java vs. C#

Command-line Interactive


Java
 
//Java use one main for command-line input or no input.
class Welcome
{
    public static void main(String[] args)
    {
        System.out.println("Hello, " + args[0]);
        System.out.println("Welcome!"); 
    }
} 

>javac Welcome.java
>java Welcome Judy
Hello, Judy
Welcome!

//interactive with user input import java.io.*; class InteractiveWelcome { public static void main(String[] args) throws Exception { // Write to console/get input System.out.print("What is your name?: "); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); String uname = in.readLine(); System.out.println("Hello, " + uname); System.out.println("Welcome!"); } } >javac InteractiveWelcome.java >java InteractiveWelcome What is your name?: Judy Hello, Judy Welcome!
String myInput; int myInt; System.out.print("Please enter a number: "); BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); myInput = in.readLine(); myInt = Integer.parseInt(myInput);

C#
 
//c# can use one of 4 Mains 
using System;
class Welcome
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello, {0}", args[0]);
        Console.WriteLine("Welcome!"); 
    }
} 
>csc Welcome.cs
>Welcome Judy
Hello, Judy
Welcome!

//interactive with user input using System; class InteractiveWelcome { public static void Main() { // Write to console/get input Console.Write("What is your name?: "); string uname = Console.ReadLine(); Console.WriteLine("Hello, {0}! ", uname); Console.WriteLine("Welcome!"); } } >csc InteractiveWelcome.cs >InteractiveWelcome What is your name?: Judy Hello, Judy Welcome!
string myInput; int myInt; Console.Write("Please enter a number: "); myInput = Console.ReadLine(); myInt = Int32.Parse(myInput);