Friday, July 1, 2011

C# Getting Started with First Console Application

Parallel to publication of Microsoft Visual Studio Express Edition, C# has now become very popular, although it is still behind VB. The concepts of object oriented programming and the whole .NET framework has made C# and friends became interesting.

Now we are going to use Microsoft Visual C# 2008 Express Edition to develop a simple console (DOS) application. I will describe the steps one by one:

  • Open Microsoft Visual C# 2008 Express Edition.
  • Create new project. You can choose between these two methods:
    • From Start Page go to Recent Projects box and double click on link Create: Project... 
    • Choose menu File - New Project...
  • In the New Project dialog choose Console Application from installed templates, insert name of the application Helloworld, and press OK.
  • A project named Helloworld will be generated and can be explored through Solution Explorer. A main function will also be generated and it is saved under Program.cs. In this file you can also find the framework for your program, contains namespace Helloworld, class Program, and static void Main(string[] args).
  • Put the code in the main function:
    • namespace Helloworld
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Console.WriteLine("WELCOME TO C#\n------------\n");
                  Console.Write("Insert your name: ");

                  string name = Console.ReadLine();
         
                  Console.WriteLine("\nHello, " + name + ", welcome to C# world!");

                  Console.WriteLine("\nPress return to exit...");
                  Console.Read();
              }
          }
      }

  • Save the project by selecting floppy button or choosing menu File - Save Program.cs.
  • Build the project by choosing menu Build - Build Solution.
  • Run the application by selecting green triangle/play button on the upper toolbar or choosing menu Debug - Start Debugging.

That is your first C# console application. Next time we will build our C# application with Graphical User Interface (GUI).

No comments:

Post a Comment