Tuesday, July 13, 2010

Writing a C# Program

Now that you’ve spent some time learning what C# is and how it fits into the .NET Framework,
it’s time to get your hands dirty and write some code. You use Visual Studio 2010 (VS) and Visual C# 2010 Express (VCE) throughout this post, so the first thing to do is have a look at some of the basics of these development environments.
VS is an enormous and complicated product, and it can be daunting to first-time users, but usingit to create basic applications can be surprisingly simple. As you start to use VS in this chapter,
you will see that you don’t need to know a huge amount about it to begin playing with C# code.
Later you’ll see some of the more complicated operations that VS can perform, but for now a basic working knowledge is all that is required.
VCE is far simpler for getting started, and in the early stages of this book all the examples are described in the context of this IDE. However, if you prefer, you can use VS instead, and everything will work in more or less the same way. For that reason, you’ll see both IDEs, starting with VS.
After you’ve had a look at the IDEs, you put together two simple applications. You don’t need to worry too much about the code in these for now; you just prove that things work. By working through the application creation procedures in these early examples, they will become second
nature before too long.
The first application you create is a simple console application. Console applications are those
that don’t make use of the graphical windows environment, so you won’t have to worry about buttons, menus, interaction with the mouse pointer, and so on. Instead, you run the application in a command prompt window and interact with it in a much simpler way.
The second application is a Windows Forms application. The look and feel of this is very familiar to Windows users, and (surprisingly) the application doesn’t require much more effort to create. However, the syntax of the code required is more complicated, even though in many cases you don’t actually have to worry about details.
You use both types of application over the next two parts of the book, with slightly more emphasis on console applications at the beginning. The additional flexibility ofWindows applications isn’t necessary when you are learning the C# language, while the simplicity of console applications enables you to concentrate on learning the syntax and not worry about the look and feel of the application.

Creating a Simple Console Application

  1. Create a new console application project by selecting File ➪ New ➪ Project in VS or File ➪ New Project in VCE.
  2. In VS, ensure that the Visual C# node is selected in the
    Installed Templates pane of the window that appears,
    and choose the Console Application project type in
    the middle pane. In VCE, simply
    select Console Application in the Templates pane. In VS, change the Location text box toC:\BegVCSharp\Chapter01 (this directory is created
    automatically if it doesn’t already exist). For both VS and
    VCE, leave the default text in the Name text box (ConsoleApplication1)
    and the other settings as they are.
  3. Click the OK button.
  4. If you are using VCE, after the project is initialized click
    the Save All button on the toolbar or select Save All from
    the File menu, set the Location field to C:\BegVCSharp
    \Chapter01, and click Save.
  5. Once the project is initialized, add the following lines of code to the file displayed in the main window:
    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    // Output text to the screen.Console.WriteLine("The first app in Beginning C# Programming!");
    Console.ReadKey();
    }
    }
    }
  6. Select the Debug ➪ Start Debugging menu item.
  7. Press any key to exit the application (you may need to click on the console window to focus on
    it first).
In VS, the preceding display appears only if the Visual C# Developer Settings are applied, as described earlier in this chapter. For example, with Visual Basic Developer Settings applied, an empty console window is displayed, and the application output appears in a window labeled Immediate. In this case, the Console.ReadKey() code also fails, and you see an error. If you experience this problem, the best solution for working through the examples in this book is to apply the Visual C# Developer Settings — that way, the results you see match the results shown here. If this problem persists, then open the Tools ➪ Options dialog and uncheck the Debugging ➪ Redirect all Output.

0 comments:

Post a Comment

 
Copyright 2010. Programming Tutorial News.