Tuesday, April 26, 2016

CSharp Your first CSharp program dissected

Your first CSharp program


Hello and Welcome to this C# multi-part series. We are going to start from the basics of C# language and get down to coding without getting deep into the history.


What you need to write and run the programs:
Download Free Visual Studio Community edition





















This is our first C# sample program.

 using System;

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

Lets dissect it for a moment line by line.


1 using System; 


This is the bare minimum required by a C# executable. The statement means I want to use the namespace "System" and everything in it. Whats a namespace? Its a logical division of an application. Your code can exist in a single name space or span many. We will get to that in the next tutorials.


2 namespace SampleConsoleApp


This is my programs namespace called "SampleConsoleApp". This namespace contains the code that I wrote. 


3 {


Starting of my namespace "SampleConsoleApp"


4   class Program

I am declaring a class called Progam. You can use any name you like. Classes are simply template for objects that you create when needed. We will discuss classes and objects later in the course as part of object oriented programming.

5   {

Start of my namespace "Program"

6      static void Main(string[] args)

This is the entry point to the program. You can have only one Main method in this type of application. The string[] args is used to pass data that your program may need to run. You can also skip writing 

7      {

Start of my Main method

8         Console.WriteLine("Hello World");

Prints "Hello World" on the console.

9         Console.ReadLine();

Makes your program to wait for an input <ENTER>.

10      }

Ending of Main method

11   }
Ending of my namespace "Program"

12 }
Ending of my namespace "SampleConsoleApp"


To run this program just hit Ctrl+F5 or goto Debug menu and select Start Without Debugging on your visual studio and you should see "Hello World" on the console window.




Happy programming.


Please feel free to post comments, like and subscribe.




No comments:

Post a Comment