Install---then Compiling and Execute your first C# Program When I first heard about Microsoft's newest language, C#, my mouth started watering. For the audience with which I mostly deal (beginner programmers trying to break into the business) I thought C# would be the great equalizer---after all, everyone (experienced programmers as well as beginners) would be learning it at the same time. Many of you may know I'm in the process of writing a C# book--this article will detail how to install it, and how to write your first program. For more, you'll need to read my book! Installing C#To install C#, for the moment anyway, you don't need to purchase a thing. You just need to get your hands on the Microsoft .Net Framework. C# is a language that comes with the Microsoft .Net (pronounced DOT-NET) Framework. The .Net Framework is an environment that permits .Net programs to run. C# is just one of many programming languages supported by the .Net framework (it even includes COBOL). Something tells me it will eventually become the most important. The .Net Framework is not an Operating System---it's more like a shell that runs within Windows. For now, the .Net Framework is available only as a Beta version (currently Beta2). That means it hasn't been released for sale. Beta versions can be flaky, and if you read the fine print at the Microsoft Website, they'll tell you NOT to install the .Net Framework on a production PC. If this warning scares you, wait until the .Net Framework production version is ready (most likely, you'll have to pay for it, although at this point no one is sure how much). The .Net Framework can be installed on a PC running Windows XP, Windows 2000 and Windows NT (sorry, Windows 98 and Windows ME users can't install the .Net Framework, and therefore can't install C#). In addition, you'll need to have Internet Explorer 5.01 or later installed, plus MDAC 2.6 (Microsoft Data Access Components). Get the .Net Framework Software Developers Kit (SDK)Right now, you can pick up the .Net Framework SDK from a variety of sources. I've received three in the mail for begin a registered owner of Visual Basic 6, and I believe I've also seen CD's included in the Visual Basic Programmers Journal. At any rate, you can access this site to download it. I should warn you the SDK is 123 Megabytes. You can download it all at once, in pieces, or get it from me if you take part in the C# class I'm offering in early 2002. Install the .Net Framework SDKOnce you have downloaded the SDK (install it in a folder of its own), double click on its Setup.exe program, and you should see a prompt similar to this one (if you don't have Internet Explorer 5.01 or higher installed, or MDAC 2.6, you will see different prompts). If you have the CD, you'll see this screenshot when you insert the CD-ROM. Otherwise, you'll see the screenshot that asks you if you wish to install the .Net Framework SDK (two down…) You don't want to install all of Visual Studio.Net--just the .Net Framework SDK, which is the part that contains the C# compiler (among other things). If you have the CD, click on 'Install only .NET Framework SDK'… This is the window you'll see next. If you downloaded the SDK from the Microsoft Website, double clicking on the Setup.exe program will result in this window being displayed also. Answer 'Yes' to begin with the installation. You should see files being copied and installed. Fairly quickly, you should see this message. Answer 'yes'. After answering 'Yes', you should see this window. On my PC, I had MDAC 2.6 already installed, but the setup program suggested MDAC 2.7, and also Microsoft Internet Information Server. For the purpose of learning C#, you can click on Ignore.
This is the window you should see next. Click on Next to continue the installation. Be sure to read the license agreement--click on the button indicating you accept the agreement, then click on the Next button to continue the installation. This is the default installation selection. You need the Software Development Kit, and I highly recommend that you install the SDK Samples as well. Click on the Next button to continue the installation. I suggest that you accept the Destination Folder--although if you are short on space on 'C', you may want to install to another drive if you have space there. Make certain that the 'Register Environment Variables' checkbox is selected--otherwise, when you try to compile your first C# program, Windows won't be able to find the compiler! You should now see this window indicating that Components are being installed. On my PC, the Component installation took about ten minutes. That's it--you'll receive this message indicating that the Installation is complete… As is typical, you'll need to restart to finalize the installation. Click the 'Yes' button to restart.
Write your first C# program After having successfully installed the .NET Framework SDK, among other things, the C# compiler will be installed somewhere in a folder on your PC. Provided you told the Installation program to update your Environmental variables, the location of the compiler should be registered properly on your PC, and you won't have to jump through any hoops to get the compiler to work. Writing your first C# program is easy--start up Notepad and enter the following code… class ILoveCSharp { Now save the program as "ILoveCSharp.cs" ( I recommend saving all of your C# programs in a folder called C#Files) Compile your first C# program Compiling your program is easy---but we will need to run the compiler from a Command Prompt. To do so, click on the Start-Run button and enter command.com in the Open Textbox… After you click on the OK button, you will see this window… which is the Command Prompt window. Type cd \C#Files to make your C#Files folder your current directory… If you enter dir in the Command Prompt Window, you should see the C# program you just saved using Notepad. Now it's time to compile your program using the C# compiler, csc. Enter csc ILoveCSharp.cs at the command prompt… You should see a message that the compiler is executing. The absence of any error messages is great news--use the 'Dir' command to verify that your C# source file has been compiled into an executable… Execute your first C# program Executing your program is easy--just enter the name of your executable at the Command Prompt… ILoveCSharp You should see the phrase 'I love C#!' appear in the window. Summary I hope that this article will help you get started with C#. For more, consider purchasing my Intro to C# book.
|