C# FAQ 2.4 - How do I build a C# application using Visual C# .NET 2003?

In the previous FAQ, you learned how use Notepad to build a simple C# application. This FAQ shows you how to develop a console-based application using Visual C# .NET.

Even though you need to work with Visual C# .NET, you have to launch Visual Studio .NET 2003 from Start | All Programs | Microsoft Visual Studio .NET 2003 | Microsoft Visual Studio .NET 2003. This is because Visual Studio .NET ships with a single IDE for developing applications with all .NET languages.

Initially, you will be presented with a welcome screen containing a list of recently opened projects. Select New | Project from the File menu. Choose "Visual C# Projects" from the Project Types section on the left-hand side and "Console Application" icon from the templates section, as shown in Figure 2.4.1.

Figure 2.4.1

You would notice from Figure 2.4.1 that I didn't change the project's name. It is set to its default name. But, while developing a real project, you should always give a meaningful name to reflect the project being done so that you can locate the project easily at a later stage of its development.

Upon clicking the OK button, a separate tab titled Class1.cs will be created on the top of the IDE. If you have more than one class file, they will be listed one by one. You also will notice that the code editor automatically creates the required template for writing a C# program, as shown in Listing 2.4.1:

Listing 2.4.1

using System;
namespace ConsoleApplication1
{
   /// <summary>
   /// Summary description for Class1.
   /// </summary>

class Class1
{
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
         //
         // TODO: Add code to start application here
         //
      }
}
}

You need not to enter complete code as you have done in FAQ 2.3. The above code calls the program's default namespace named ConsoleApplication1, Main() method and the required documentation comments as discussed in FAQ 2.3. The IDE also creates a class named Class1. This name can be modified from the Solution Explorer window. The STAThread attribute in the code denotes your application's threading model; you will use this attribute when you work with COM Interoperability.

Your next task is to write the required statements for running the application by deleting the TODO comment portion and entering the following code:

Console.WriteLine("Welcome to C-Sharp");

You will also notice that Visual C# .NET automatically colors the syntaxes and also shows Intellisense when entering the code, as shown in Figure 2.4.2.

Figure 2.4.2

The preceding program is now ready to be compiled. Press F5 to build the application. When you build an application using Visual C# .NET, it compiles the program and also automatically executes the program. Alternatively, you can build your project by selecting the Debug | Start menu from the IDE.

For the preceding program, Visual C# .NET displays the command window and closes it instantly. It can be rectified by adding the following code just below the statement you have entered above:

Console.Read();

If you build the project now, Visual C# .NET displays the command window, as shown in Figure 2.4.3.

Figure 2.4.3

Visual C# .NET automatically creates the required assembly and manifest files in the background. If there are any errors, it will be displayed on the Task List window on the bottom of the IDE. Double-clicking an entry on the task list takes you to the specified location of an error. For instance, if you remove a semicolon, a red mark will appear in the specified location. The task list reads as "; expected". The entries on the task list will immediately disappear as soon as you rectify the relevant error.



About the Author

Anand Narayanaswamy

Anand Narayanaswamy (Microsoft MVP) is a freelance writer for Developer.com and Codeguru.com. He works as an independent consultant and runs NetAns Technologies (http://www.netans.com)which provides affordable web hosting services. He is the author of Community Server Quickly (http://www.packtpub.com/community-server/book). Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes product and book reviews for various websites. He can be reached at ananddotnet@yahoo.co.in

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds