Creating Console Applications


As a first step, you can create a simple console application using Visual C#. Because this is your first application, you need to create a blank solution (shown in Figure 5.4) and give it a name (in this case, call it Chapter-5).

Figure 5.4. Creating a blank Visual Studio .NET solution.

Now that a blank solution is added, right-click the solution and select Add, New Project. Because you are creating a console application in Visual C#, select Visual C# Projects, Console Application (shown in Figure 5.5). You can use the predefined name ConsoleApplication1.

Figure 5.5. Creating a Visual C# console application project.

(Normally for solutions, projects, classes, and namespaces, you should use appropriate names that have a defined correlation with the business project they are solving, but because this is your first entry, you can use the defaults).

With the Console Application project, a class called Class1 is created in the namespace ConsoleApplication1, which has the traditional static Main method ”the entry point when the application is executed. Following the main method is a comment that represents a TODO for the developer, to write the actual logic for the application. You can just write the simplest statement:

 
 System.Console.WriteLine("First Console Application using Visual Studio .NET"); 

The code snippet for the completed Class1 is as follows (shown in Figure 5.6):

 
 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                //  System.Console.WriteLine("Using Visual Studio .NET");  }      } } 
Figure 5.6. A console application project, Class1, within the Visual Studio .NET editor.

The code in bold represents the only code that you have written. You must have also noticed that IntelliSense, which completes the code as you type, was in effect when you typed the statement ( System ) as well. Now that you have written the code, save it (keyboard shortcut: Ctrl+S) and build the solution (Ctrl+Shift+B or Build, Build Solution). The Build Output window will echo any errors if there were any typographical or syntax errors. The application can then be executed (without debugging) by Debug, Start Without Debugging or Ctrl+F5 (see Figure 5.7).

Figure 5.7. Result from running the console application.

As you can tell from the preceding illustration, even creating a simple application becomes easier to develop, build, and test when an Integrated Development Environment such as Visual Studio .NET is applied.



Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net