In the previous FAQ, you learned how to build a simple application using
Notepad. 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 15.1 {View
Enlarged Picture}You would notice from the above figure that I didn’t change the name
of the project. 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 will also notice that the code
editor automatically creates the required template for writing a C#
program as shown in listing 15.1:
Listing 15.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
//
}
}
}
The above code calls the program’s default namespace named
ConsoleApplication1, Main() method and the required documentation
comments as discussed in one of the previous FAQ. 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 applications
threading model and you will use this attribute at the time of working
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 while entering the code as shown in
the figure shown here.
The above program is now ready to get 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 our above 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 the figure shown on the left side.
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 on 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.
How much time will it take to install Visual Studio .NET?
It entirely depends upon your system
configuration such as the processor speed and RAM. It would be better if
you install it on a system with at least 512 MB RAM.
|