Monday, 16 September 2013

Console Application : Hello World
Before Starting Windows Application in C# , We Have To Learn Console Application Programs.

We Can Start With Hello World Program Procedure.

To create and run a console application

  1. Start Visual Studio.
  2. On the menu bar, choose File, New, Project.


    The New Project dialog box opens.
  3. Expand Installed, expand Templates, expand Visual C#, and then choose Console Application.
  4. In the Name box, specify a name for your project, and then choose the OK button.



    The new project appears in Solution Explorer.
  5. If Program.cs isn't open in the Code Editor, open the shortcut menu for Program.cs in Solution Explorer, and then choose View Code.
  6. Replace the contents of Program.cs with the following code.

     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Hello_World
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");

                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();

            }
        }
    }

    Choose the F5 key to run the project. A Command Prompt window appears that contains the line Hello World!

    My Output


     

    Important :
    
    
    A C# console application must contain a Main method, in which control starts and ends. The Main method is where you create objects and execute other methods.
    The Main method is a static (C# Reference) method that resides inside a class or a struct. In the previous "Hello World!" example, it resides in a class named Hello. You can declare the Main method in one of the following ways:
    • It can return void.
      static void Main()
      {
          //...
      }
      
    • It can also return an integer.
      static int Main()
      {
          //... 
          return 0;
      }
      
    • With either of the return types, it can take arguments.
      static void Main(string[] args)
      {
          //...
      }
      
      -or-
      static int Main(string[] args)
      {
          //... 
          return 0;
      }
      
    The parameter of the Main method, args, is a string array that contains the command-line arguments used to invoke the program. Unlike in C++, the array does not include the name of the executable (exe) file.

    The call to ReadKey at the end of the Main method prevents the console window from closing before you have a chance to read the output when you run your program in debug mode, by pressing F5.
    C# programs generally use the input/output services provided by the run-time library of the .NET Framework. The statement System.Console.WriteLine("Hello World!"); uses the WriteLine method. This is one of the output methods of the Console class in the run-time library. It displays its string parameter on the standard output stream followed by a new line. Other Console methods are available for different input and output operations. If you include the using System; directive at the beginning of the program, you can directly use the System classes and methods without fully qualifying them. For example, you can call Console.WriteLine instead of System.Console.WriteLine:
    using System;
    
    Console.WriteLine("Hello World!");
     
     
     
    All The Best For Your First Assignment. :) 

       

No comments:

Post a Comment