Search This Blog

Wednesday, August 10, 2011

C# 4.0 – Ch:02:A – Language Basics


If you like this article, please click on +1 button and share with your friends.

Click here for the Video Tutorial of this article.
visit www.iGnani.com

…. continued from previous article “C# 4.0 – Ch:01:E – Assemblies

First program in C#.NET 4.0

In this chapter, we shall start with writing our first program in C#.NET 4.0.

In this tutorial, we will be working in Visual Studio 2010. Visual Studio 2010 is a fully integrated development environment from Microsoft, designed to make the process of writing code, debugging it and compiling it to an assembly to be shipped as easy as possible. Though there are other ways to build C# programs, but Visual Studio is the most widely used and it’s also freely available as an express edition, so we’ll stick with that.

Note: If you don’t have Visual Studio, you can download the free Express edition from http://www.microsoft.com/express/.

I am going to keep this article as simple as possible. Don’t worry if you don’t know how to use Visual Studio 2010 or the Express edition. Though I am not going to cover Visual Studio in detail in this tutorial, I will explain the important features and how to make use of them as we progress through this article. For now, all you need to do is try it out as explained below without worrying about the details.

In the first part of this chapter, we will create a very simple program using the bare minimum steps required to develop your first C#.NET application. We will as usual start with a “Hello World!” program.

For the rest of this tutorial, I will be referring to Visual Studio 2010 / Visual Studio 2010 Express Editions simply as Visual Studio or VS and all our samples, images and videos show Visual Studio 2010 while there will not be much of a difference between these editions. You can find out about the limitations of Express editions at http://www.microsoft.com/express/.

Make sure you have your Visual Studio up and running. Since console applications perform all their input and output at the command line, they are ideal for quickly trying out language and we will also start out with the Console application.

Create a C# Console Application

  1. On the File menu, click New and then from the submenu that appears select Project. The shortcut for this is Ctrl+Shift+N. The New Project dialog box appears as shown below.

    NewProject

    You can see the various templates that are available. What is important for us here is that you need to make sure that in the left hand pane “Visual C#” and under that “Windows” is selected.

  2. In the dialog’s middle pane, select the “Console Application” template as the project type. This creates an old fashioned command-line application that runs in a console window. It might not be the most exciting kind of program, but it’s the easiest to create and understand, so that’s where we’ll start.
  3. You need to pick a name for your project. By default, Visual Studio will suggest the name as ConsoleApplication1. Let us change it to Demo1. In the Name field at the bottom of the dialog, type Demo1.

  4. Visual Studio also wants to know where you would like to save the project. You can save it wherever you like. The default location should be fine, but you can always enter a new path if you want. For this video, I will change it as its in the image above.

  5. Don’t worry about the remaining things. Click “OK”.
    Visual Studio creates a new folder for your project, which will be same as the Name of your project. It also opens the main Visual Studio window and the Code pane where you will enter and modify the C# source code that creates your application.

    DefaultView

  6. Notice the toolbar at the top of the window. This toolbar contains icons for Creating, Opening and Saving projects, Editing source code, Building your project, and Searching your code, while the last five icons at the far right of the first toolbar are used to open important windows such as Solution Explorer, Properties Windows, Toolbox. Place the mouse pointer over any one of these icons to obtain pop-up tooltip Help.

  7. On the right hand side of the above image is a Solution Explorer. Solution Explorer is a very useful pane as it displays the various files that make up your project. The most important file in this project is the file "Program.cs" which contains the source code for your application.

  8. The main part of the above image contains the area used to design and write code. In the above image, you can see the file “Program.cs” opened. Visual Studio has generated some code. This code doesn’t do anything yet.

  9. We had set out to create a “Hello World!” project and we need to add just a single line of code to finish our project.

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, C# World!");
        }
    }



  10. As shown in the above, make the changes to your code. The changes are marked in bold and also the line highlighted.
  11. Our project is now ready to be executed. From the Debug menu select the “Start Without Debugging” item, or just press Ctrl-F5. The program will execute.
    Since this is a console application, a console window will open. The first line of this window will contain the text “Hello, C# World!” and the next line will be a prompt saying “Press any key to continue...”. Congratulations!, You just completed your first program in C#.NET 4.0, built using Visual Studio. Once you are done admiring your work, press a key to close the window.

    Note: If you use “Start Debugging” or F5 instead of “Start Without Debugging”, it will run the code, but in Visual Studio’s debugging mode, which doesn’t keep the window open once the application has finished. This way you won’t be able to see the console and the text in it, since it will most likely run to completion and then close the window even before you’ve had a chance to see the output.





…next article “C# 4.0 – Ch:02:B – Hello C# World : Up-Close




visit www.iGnani.com


MicroMind Information Systems
#4013, K.R. Road, Banashankari II Stage,
BANGALORE - 560070
KARNATAKA, INDIA
Phone: +91 80 26762747



No comments:

Post a Comment