My Adsense

Saturday, October 24, 2009

Introduction to C# : A hands on approach



       In this lesson you will be familiar with C# applications.

            Objectives

            After completing this unit you should have a clear concept on:

*        Visual Studio 2008
*        Anatomy of a C# program
*        Visual Studio Templates
*        Console application
*        Desktop application

You should be able to create:

*        Console application
*        Desktop application


Topics

In this lesson, you will learn about the following topics:
*        Visual Studio
*        Templates
*        C# program structure
*        Console applications
*        Desktop applications











Visual Studio
Microsoft Visual Studio is an Integrated Development Environment (IDE) created by Microsoft Corporation for Windows Application Development. This IDE is used to develop console and graphical user interface applications run able only in Windows Platform.
Visual Studio 2008 is one of the versions of Visual Studio. Some other versions are Visual Studio 2005 or Visual Studio 2010 or Visual Studio Team System.
Visual Studio provides some features to its users. Some of these are:
·         Code editing
·         Debugging the code
·         Designing the User Interface
·         Some helping tools
·         Extensibility
Visual Studio has some built in compilers integrated in it. The main languages are C# and Visual Basic. But one can create any application in another language such as J#, C++ etc.

Templates
Visual studio project and item templates provide reusable and customizable project and item stubs that accelerates the development process, removing the need to create new projects and items from scratch.
Depending on the instance of the Visual Studio, generally there are 2 types of templates we can see. They are:
·         Project templates
§  Windows
§  Web
§  Smart Device
§  Office
§  Database
§  Reporting
§  Test
§  WCF
§  Workflow
·         Item templates
§  General
§  Web
§  Script


Walkthrough 1: Visual Studio Project Template Familiarization

To make the project templates more clear, now we will view some templates of Visual Studio 2008.

Steps:
  1. Start Microsoft Visual Studio 2008
  2. Click File>New>Project

You will see the following window.


Figure: Project Template


Walkthrough 2: Visual Studio Item Template Familiarization

To make the item templates more clear, we will now view some item templates of Visual Studio 2008.

Steps:
  1. Start Microsoft Visual Studio 2008
  2. Click File>New>File
You will see the following window.



Figure: Item Template

C# Program Structure
Class library:
Like all other programming language, C# also follows some rules. At first, a sample program needs some system library classes. These libraries help the compiler to compile the code.
For example:
            using System.Text;
If this is not written at the beginning of a class structure, one can’t use the regular expression functionalities under that class.
Namespace:
A project can have multiple files. To organize these classes, multiple folders can be created. The conceptual hierarchy is called the namespace.
For example:
namespace AnatomyOfCSharpClass
This is a sample namespace of a project.           
Main Method:
Main method is the entry point of any kind of high level language. C# is not indifferent from that. A C# project must have a main() method. The code written for a main method is:
static void Main(string[] args)

Console Applications
A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal, the command line interface of some operating systems (Unix, DOS, etc.) or the text-based interface included with some Graphical User Interface (GUI) operating systems, such as the Win32 console in Microsoft Windows.
A user typically interacts with a console application using only a keyboard and display screen, as opposed to GUI applications, which normally require the use of a mouse or other pointing device. Many console applications such as command line interpreters are command line tools, but numerous Text User Interface (TUI) programs also exist.


Walkthrough 3: Creating a console application

To make the discussion more clear, we will now create a very small console application. This application will show “Hello World” into the console.

Steps:
  1. Start Microsoft Visual Studio 2008
  2. Click File>New>Project
You will see the following window.

     
Figure: New Project Creation Template

  1. Select Windows>Console Application
  2. Write appropriate name in ‘Name’ box, change the destination folder of the project directory by clicking the ‘Browse’ button
  3. Click ‘Ok’ button
  4. You will see the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WalkThrough1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
  1. Now write the following code snippet into the main method
     Console.WriteLine("Hello world");
     Console.ReadKey();
  1. Now Press F5 or Click on the Debug button

            Figure: Debug Icon
  1. You will see the output given below

Figure: Console Output 


Desktop Application
An application (desktop) is a computer program designed to help people perform a certain type of work. In a desktop application users can interact with the application via both keyboard and mouse.


Walkthrough 4: Creating a desktop application


Now we will create a very simple desktop application. In this application, user will click on a button ‘Show’ and a messagebox (a small popup box) will appear displaying ‘Hello World’.

Steps:
1.      Start Microsoft Visual Studio 2008
2.      Click File>New>Project
3.      Select Windows>Windows Forms Application
4.      Give an appropriate name and others as the previous walkthrough
5.      Click ‘Ok’ button

 You will get the following picture in your window

Figure:Default Windows Form
           
6.      Now drag and drop a button from toolbox situated at the left side of your development window.
7.      Click on the button
8.      Change the Text Property of the button into ‘Show’

This will looks like the following picture.



Figure: Text Property of a TextBox Control      

9.      Double click on the button of the form and write the following code snippet
     private void button1_Click(object sender, EventArgs e)
     {
         MessageBox.Show("Hello World");
  }
10.  Now run the application bu clicking on the debug button or pressing F5
11.  Click on the ‘Show’ button of the running application
12.  A messagebox will appear. You will get the following picture


Figure: Output of the desktop application


Practice:
Create a desktop application like the following figure.

Figure: Information Management Application User Interface    
    
     Features:
·         After clicking on the save button, all of the information of the textbox will be disappeared.
·         Button names are self descriptive. Relevent information will be shown in a messagebox when a button is clicked.

[For advanced users]
Create a calculator which will take two number in two different textboxes. It has four buttons (Add, Subtract, Multiply, Divide). Calculated result will be showed in a label when the button is clicked.

No comments: