Create a Console Application from the Command Line - CSharp Language Basics

CSharp examples for Language Basics:Console

Introduction

In one of your classes, ensure you implement a static method named Main with one of the following signatures:

public static void Main();
public static void Main(string[] args);
public static int Main();
public static int Main(string[] args);

Build your application using the C# compiler (csc.exe) by running the following command:

csc /target:exe HelloWorld.cs

To build a console application consisting of more than one source code file:

csc /target:exe /main:HelloWorld /out:MyFirstApp.exe HelloWorld.cs ConsoleUtils.cs

Demo Code

using System;/*from   w w  w. jav  a 2  s . c  o  m*/
public class ConsoleUtils
{
   // A method to display a prompt and read a response from the console.
   public static string ReadString(string msg)
   {
      Console.Write(msg);
      return Console.ReadLine();
   }
   // A method to display a message to the console.
   public static void WriteString(string msg)
   {
      Console.WriteLine(msg);
   }
   // Main method used for testing ConsoleUtility methods.
   public static void Main()
   {
      // Prompt the reader to enter a name.
      string name = ReadString("Please enter your name : ");
      // Welcome the reader to Visual C# 2010 Recipes.
      WriteString("Welcome to Visual C# 2010 Recipes, " + name);
   }
}

Result


Related Tutorials