Creating Our First C Program

Creating Our First C# Program

Welcome to C#. This is a programmer-to-programmer book, so we're going to pack in the code, pushing the envelopeand not wasting much space. In this book, we're assuming you know programming already, but are new to C#. In these early chapters, we're going to cover the C# basicsnot because you're unfamiliar with elementary constructs like variables and if statements, but because there are substantial differences between C# and languages like C++ and Java that can trip you up unless you know what's going on. Please feel free to skim this early material and press on to the later chapters if you want. If you're a C++ programmer, you might take a look at the "For C++ Programmers" sidebars as you skim; these sidebars assume you know C++ and often give you information at a more advanced programming level than the surrounding text. In the following chapters, we're going to break C# wide open as we see what it can do for you.

We'll get our start with the code immediately, digging right into things. You can see our first C# application, ch01_01.cs, in Listing 1.1 (all the code in this book is available for downloadsee the Introduction for the details). As you can surmise from this code, this little application displays the friendly greeting "Hello from C#." when it runs.

Listing 1.1 The First C# Application (ch01_01.cs)
 class ch01_01 {   static void Main()   {     System.Console.WriteLine("Hello from C#.");   } } 

Let's get this application to run and then we'll take it apart line by line.

Compiling the First Program

There are two ways to compile our first C# applicationusing the C# command-line compiler, csc.exe, and using the Visual Studio .NET development tool. We'll take a look at both techniques here. Because csc is free from Microsoftit's built into the .NET Frameworkit's an attractive option for many programmers. On the other hand, building Windows and Web services is very difficult using only csc, so we will use Visual Studio .NET primarily to build those kinds of applications later in the book. Until that point, our code can be used with either csc or Visual Studio .NETjust download the code samples from the Sams Web site at http://www.samspublishing.com. You also can type it into a text editor like WordPad, or copy and paste it directly into Visual Studio .NET, as we're about to see.

To use the C# command-line compiler, csc, you need to install the .NET Framework's Software Development Kit (SDK), which you can download for free at http://msdn.microsoft.com/downloads.

After you've installed this download, you can find the csc command-line compiler in your system's root directory; for example, in Windows 2000, for example, that is C:\WINNT\Microsoft.NET\Framework\ xxxxxxxx \csc , where xxxxxxxx is the version of the .NET Framework that you've downloaded. You can add csc to your computer's path so that you can type it directly at the command line, or you can enter its full path (that is, C:\WINNT\Microsoft.NET\Framework\ xxxxxxxx \csc ) each time you run it.

WARNING ON MICROSOFT URLS

Note that Microsoft has the regrettable habit of changing their URLs every few months, so by the time you read this, you might have to go to a different URL than http://msdn.microsoft.com/downloads to download the .NET Framework SDK. In that case, search the http://msdn.microsoft.com site for ".NET Framework Software Development Kit."


To compile our first program, enter the code in Listing 1.1 into a text file named ch01_01.cs, or download it from the Web site for this book. Then open a command-line window (that is, a DOS windowin Windows 2000, you do that with the Start, Programs, Accessories, Command prompt; in Windows XP, it's Start, All Programs, Accessories, Command prompt). Assuming you've added csc to your computer's path, you can simply move to the directory that holds ch01_01.cs and compile it like this (for a list help topics, type csc /? ):

 
 C:\>csc ch01_01.cs Microsoft (R) Visual C# .NET Compiler version xxxxxxxxxxx for Microsoft (R) .NET Framework version xxxxxxxx Copyright (C) Microsoft Corporation 2001-2002. All rights reserved. 

SETTING THE PATH VARIABLE

To set your system's Path variable, open the Control panel, double-click System, click the Advanced tab, and then click the Environment Variables button. Double-click the entry for the Path system variable and add the directory that holds csc to that variable (note that paths are separated by semicolons).


RUNNING THE VISUAL STUDIO GROUP COMMAND PROMPT

Alternatively, if you have Visual Studio installed and want to run the command-line compiler, you can run the Visual Studio command prompt, which already has the environment variables set correctly. Just select Start, Start, Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools, Visual Studio .NET Command Prompt menu item.


This compiles ch01_01.cs into ch01_01.exe, which is what we'll need.

If you have it, you can also use Visual Studio .NET to compile ch01_01.cs into ch01_01.exe. To do that, start Visual Studio .NET now and select the File, New Project to open the New Project dialog box you see in Figure 1.1.

Figure 1.1. Creating a new project in Visual Studio .NET.

graphics/01fig01.jpg

In the New Project dialog box, select the Visual C# Project folder in the Project Types box, and the Console Applications item in the Templates box. Then enter the name of the new project, ch01_01, in the Name text box and the location where you want to store the project files in the Location box, as you see in Figure 1.1. Click OK to create the new project.

This opens the Visual Studio Integrated Development Environment (IDE), which you see in Figure 1.2. We'll see more on the IDE when it's time to start building Windows applications in Chapter 7, "Creating C# Windows Applications."

Figure 1.2. The Visual Studio Integrated Development Environment.

graphics/01fig02.jpg

When the IDE appears, the code designer window in the center will hold some skeleton code for a console application. Just replace that code with the first application's code, as you see in Figure 1.3. Next , in the Solution Explorer window, shown in the upper-right corner in Figure 1.2, find the entry for the default .CS file in this project, which will be Class1.cs, and change it to ch01_01.cs by editing that name directly (as you would edit a filename in the Windows Explorer). See Figure 1.3.

Figure 1.3. The code in the IDE.

graphics/01fig03.jpg

NAMING SOURCE CODE FILES

Here, we've changed the name of the .CS file to match the name of the class contained in it, ch01_01 . That's not necessary in C#, as it is in Javawe could have left the filename as Class1.cs. It's often good programming practice to make a .CS filename match the class it contains, but you don't have to do it that way. For the console applications we develop in this book, all you really have to do is replace the default code Visual Studio supplies and then run the project.


That's all it takesnow we're ready to run this new project.

Running the First Program

To run ch01_01.exe after compiling it with the command-line compiler, just enter ch01_01 at the command prompt. The new application will run and display its message like this:

 
 C:\>ch01_01 Hello from C#. 

That's how you run console applications (if you have problems ending a C# console application, type ^C to break out of it).

To run the example from the Visual Studio .NET IDE, select the Debug, Start Without Debugging menu item, which displays the example at work in a DOS window, as you see in Figure 1.4. Note that the application also automatically displays the prompt "Press any key to continue"; when you do press a key, the DOS window closes .

Figure 1.4. Running the first application.

graphics/01fig04.jpg

On the other hand, if you select Debug, Start (not Debug, Start Without Debugging), or run the ch01_01.exe file created by Visual Studio .NET outside Visual Studio (by double-clicking ch01_01.exe in the Windows Explorer, for example), that prompt isn't displayed. The DOS window appears only briefly , flickering onscreen and then immediately closing. To display a prompt to the users and wait until the users type a key, you can add this code, which uses ReadLine to wait for user input:

 
 class ch01_01 {   static void Main()   {     System.Console.WriteLine("Hello from C#.");  System.Console.WriteLine("Press any key to continue");   System.Console.ReadLine();  } } 

When you add this code, you can select Debug, Start in the IDE to run the example. You can also create ch01_01.exe as a stand-alone executable by selecting Build, Build Solution, and then creating ch01_01.exe in a subdirectory of the ch01_01 project directory. (The actual subdirectory depends on several Visual Studio settings, such as whether you're creating a debug-enabled version of your program.) Then you can run ch01_01.exe at the command promptthe result is the same as when you use the command-line compiler.

And that's ityour first C# program is running.

The next step is to understand C#'s place in the programming world. That programming world starts with the .NET Framework, of which C# is a part.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

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