03 C# Program Structure

Well, it’s the time to start the real technical sessions but before we start you will need to run your code and to run and execute  your programs you will have to Download and Install Visual C# Express 2010 or Visual Studio (any version)
and you can Download Visual C# Express 2010 from the link below:
http://www.visualstudio.com/downloads/download-visual-studio-vs

And to execute any C# program you need:

  1. Text editor
  2. Compiler
  3. and then, it will be executed.

So, let’s See and analyse a C# Hello World Example and take it as a reference.

Any C# Program basically consists of  the following:

  • Namespace Declaration
  • A Class (Methods and Attributes)
  • Main Method
  • Statements & Expressions
  • Comments (Optional)

So, let’s see the C# code of the Hello World Example:

using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorldApplication
{
 class HelloWorld
 {
  static void Main(string[] args)
  {
    /* my first program in C# */
   Console.WriteLine("Hello World");
   Console.ReadKey();
  }
 }
}

So, the output of this program when it’s executed will be :    Hello World

Time to analyse the code lines of that program.

  • The first line using system; –> the using keyword is used to include the System namespace in the program. A program generally has multiple using statements. 
  • The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.
  • The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally would contain more than one method. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.
  • The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class will do when executed.
  • The next line /*…*/ will be ignored by the compiler and it has been put to add additional comments in the program (optional).
  •  The Main method specifies its behavior with the statement Console.WriteLine(“Hello World”);
    WriteLine
    is a method of the Console class defined in the System namespace. This statement causes the message “Hello, World!” to be displayed on the screen. 
  •  The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.

And now you should know these important points:

  • C# is a Case Sensitive which means it distinguish between lower and capital letters.
  • All statements and expression must end with a semicolon (;).
  • The program execution starts at the Main method.

Time to Compile and Execute the program:
Kindly, Follow these steps 🙂

  1. Start Visual Studio.
  2. On the menu bar, choose File, New, Project.
  3. Choose Visual C# from templates, and then choose Windows.
  4. Choose Console Application.
  5. Specify a name for your project, and then choose the OK button.
  6. The new project appears in Solution Explorer.
  7. Write code above in the Code Editor.
  8. Click the Run button or the F5 key to run the project. A Command Prompt window appears that contains the line Hello World.

And to write this program but in Windows Form it will be different a little, Just follow these steps:

  1. Start Visual Studio.
  2. On the menu bar, choose File, New, Project or click (ctrl+shift+n).
  3. Choose Visual C# from templates, and then choose Windows.
  4. Choose Windows Form Application.
  5. Specify a name for your project, and then choose the OK button.
  6. This windows will be opened.
    1
  7. From the Toolbox find a Button Drag and Drop it in your Form that Appears or Double click it.
  8. Then, you will find the Button has been added to your form, from Properties Windows on your right you’ll find it and search Text and change it to Hello World and then, Double click the Button.
  9. Another window will appear, Write the following code and then press Start.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello");
        }
    }
}

And now we have already finished our first technical C# posts stay tuned for more and if you need any help just feel free to contact me 🙂
Thanks for your appreciated time 🙂

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s