|
Related Reading
|
Find out how to write your first C# program in this book excerpt from Learning C#. This is the first of two excerpts on getting started with C#.
You can use C# to create three different types of programs:
The .NET platform is web-centric. The C# language was developed to allow .NET programmers to create very large, powerful, high-quality web applications quickly and easily. The .NET technology for creating web applications is called ASP.NET.
ASP.NET, the next generation from ASP (Active Server Pages), is composed of two Microsoft development technologies: Web Forms and Web Services. While the development of fully realized web applications using these technologies is beyond the scope of this book, learning the basics of the C# language will certainly get you started in the right direction. C# is generally acknowledged to be the language of choice for ASP.NET development.
|
In This Series
Getting Started with C#, Part 2 |
Typically, you'll create an ASP.NET application when you want your program to be available to end users on any platform (e.g., Windows, Mac, Unix). By serving your application over the Web, end users can access your program with any browser.
When you want the richness and power of a native application running directly on the Windows platform, alternatively you might create a desktop-bound Windows application. The .NET tools for building Windows applications are called Windows Forms; a detailed analysis of this technology is also beyond the scope of this book.
However, if you don't need a Graphical User Interface (GUI) and just want to write a simple application that talks to a console window (i.e., what we used to call a DOS box), you might consider creating a console application. This book makes extensive use of console applications to illustrate the basics of the C# language.
Web, Windows, and console applications are described and illustrated in the following pages.
|
|
|
|
|
|
TIP: This book does not go into all the myriad details of building robust Windows and ASP.NET applications. For complete coverage of these topics, please see Programming ASP.NET and Programming .NET Windows Applications, both by Jesse Liberty and Dan Hurwitz (O'Reilly).
|
A program consists of English-language instructions called source code. The syntax for these instructions is strictly defined by the language. Source code consists of a series of statements. A statement is an instruction to the complier. Each instruction must be formed correctly, and one task you'll face when learning C# will be to learn the correct syntax of the language. For example, in C# every statement ends with a semi-colon.
Each instruction has a semantic meaning that expresses what you are trying to accomplish. Although you must follow the syntax, the semantics of the language are far more important in developing effective object-oriented programs. This book will provide insight into both the syntax and the semantics of good C# programs.
Save the source code you write in a text file. You can write this source code file using any simple text editor (such as Notepad), or you can use the Visual Studio .NET Integrated Development Environment (IDE). Visual Studio .NET is described in Chapter 4.
Once you write your program, you compile it using the C# compiler. The end result of compiling the program is an application.
In this chapter, you will create a very simple application that does nothing more than display the words "Hello World" to your monitor. This console application is the traditional first program for learning any new language; it demonstrates some of the basic elements of a C# program.
Once you write your "Hello World" program and compile it, this chapter will provide a line-by-line analysis of the source code. This analysis gives something of a preview of the language; Chapter 5 describes the fundamentals much more fully.
As explained earlier, you can create C# programs with any text editor. You can, for example, create each of the three programs shown previously (in Figure 2-1, Figure 2-2, and Figure 2-3) with Notepad. To demonstrate that this is possible, you'll write your very first C# program using Notepad.
Begin by opening Notepad and typing in the program exactly as shown in Example 2-1.
Example 2-1: Hello World in Notepad
namespace NotePad
{
class HelloWorld
{
// every console app starts with Main
static void Main()
{
System.Console.WriteLine("Hello world!");
}
}
}
That is the entire program. Save it to your disk as a file called helloworld.cs.
We'll examine this program in some detail in just a moment. First, however, it must be compiled.
Once you save your program to disk, you must compile the code to create your application. Compiling your source code means running a compiler and passing in the source code file. You run the compiler by opening a command prompt (DOS box) and entering the program name csc. Then you pass in your source code file by entering the filename on the command line, as in the following:
csc HelloWorld.cs
The job of the compiler is to turn your source code into a working program. It turns out to be just slightly more complicated than that because .NET uses an intermediate language called Microsoft Intermediate Language (MSIL, sometimes abbreviated to IL). The compiler reads your source code and produces IL. The .NET Just In Time (JIT) compiler then reads your IL code and produces an executable application in memory.
Microsoft provides a command window with the correct environment variables set. Open the command window by selecting the following menu items in this order:
Start -> Programs -> Microsoft Visual Studio .NET
-> Visual Studio.NET Tools -> Visual Studio .NET Command Prompt
Then navigate to the directory in which you created your code file and enter the following command:
csc helloworld.cs
The Microsoft C# compiler compiles your code; when you display the directory you'll find the compiler has produced an executable file called helloworld.exe. Type helloworld at the command prompt, and your program executes, as shown in Figure 2-4.
|
|
Presto! You are a C# programmer. That's it, close the book, you've done it. Okay, don't close the book--there are details to examine, but take a moment to congratulate yourself. Have a cookie.
Granted, the program you created is one of the simplest C# programs imaginable, but it is a complete C# program, and it can be used to examine many of the elements common to C# programs.
In the next installment, examine your first program.
Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.
Return to .NET DevCenter
Copyright © 2009 O'Reilly Media, Inc.