Friday, 19 February 2016

Introduction to c#


In this session we will discuss on
             Basic structure of a c# program
             Compiling & executing a c# program    
      
Basic structure of a c# program:

using System;     // namespace declaration
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}            

        Namespace declaration indicates that we are using System namespace.We can use classes in .net framework.We can input and output data to command prompt using Console class.

       WriteLine is a method of the Console class and displays the message "Hello, World!" on the screen.

       using System  is a namespace  which tells to the left of the program that we are  going to make use of the code in the System .Here the console class is present inside the namespace system.

Namespace: It is a collection of classes,interfaces,enums,structs and delegates.It is used to organise your code.

      It is not necessary to use  namespace declaration .we can avoid it by using fully qualified name as below.


class Program
{
static void Main()      // method
{
System.Console.WriteLine("Hello, World!");
}
}         
       Main method is the entry point of your application.

Compiling  & executing a program:

         To run C# code, Visual Studio is the best editor. Either you can choose console based application to run program directly or you can write program on notepad and then run them on visual studio command prompt.
If you are using Visual Studio.Net for compiling and executing C# programs, take the following steps:

  • Start Visual Studio.
  • On the menu bar, choose File -> New -> Project.
  • Choose Visual C# from templates, and then choose Windows.
  • Choose Console Application.
  • Specify a name for your project and click OK button.
  • This creates a new project in Solution Explorer.
  • Write code in the Code Editor.
  • Click the Run button or press F5 key to execute the project. A Command Prompt window appears that contains the line Hello World.

You can compile a C# program by using the command-line instead of the Visual Studio IDE:

  • Open a text editor and add the above-mentioned code.
  • Save the file as helloworld.cs
  • Open the command prompt tool and go to the directory where you saved the file.
  • Type csc helloworld.cs and press enter to compile your code.
  • If there are no errors in your code, the command prompt takes you to the next line and generates helloworld.exe executable file.
  • Type helloworld to execute your program.
  • You can see the output Hello World printed on the screen.
Congratulations,you have just created your first C# application.
  

0 comments:

Post a Comment