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

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

  • On-demand webcast Lately it seems that everywhere you turn, there's another cybersecurity breach — and hackers and thieves are never satisfied with the status quo, continuing to refine their tactics or create new methods of attack. So how do you protect your business now, but also plan for your future security needs? How can you guard against this ever-changing threat landscape? Watch Jeremy Smolik, Systems Engineer at Kaspersky Lab North America, in this on-demand webinar as we explore the biggest …

  • Chuze Fitness is a fast-growing fitness chain with over 21 locations spanning California, Arizona and Colorado. Chief information and marketing officer, Kris Peterson, explains why access to fast and reliable Wi-Fi is a "must have" service at their gyms and why they switched to Ruckus Cloud Wi-Fi. Chuze Fitness needed to provide a good user experience to the hundreds of guests streaming music, podcasts and videos as they worked out. They also needed to adequately cover their sprawling 20-40,000 square foot …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date