WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

You Can Hack .NET Without Buying Visual Studio .NET

by Brian Jepson
02/11/2002

C# Essentials, 2nd Edition has been updated to cover the final release of Microsoft's new C# programming language. It packs a lot of information into a small space so you can be up to speed with C# in a few short sessions. So, grab the book, your favorite hot or cold beverage, and Microsoft's C# compiler, and start learning. About that C# compiler—we're talking about Microsoft here, right? Let's break this down (with apologies to MasterCard):

  • A hot beverage: $1.25
  • A cold beverage: $2.50
  • Microsoft Visual Studio .NET Professional after rebates: $700
  • A great book that distills everything you need to know about the C# language: priceless

If you've seen those MasterCard ads, you probably saw that last one coming. But what about that third item? It looks painful, I know. But I'm here to make one thing perfectly clear:

You do not need Visual Studio .NET to hack in C#.

It's true that Visual Studio .NET comes with a compelling set of features. You can develop and deploy a Web service with a few taps on the keyboard and a handful of mouse clicks. But if you're just trying to learn the language, you can can download the .NET Framework SDK for free, find a good editor, and just start writing code.

About the .NET Framework SDK

The .NET Framework SDK comes with compilers (C#, JScript.NET, VB .NET, and Managed C++), the .NET Framework Class Library (from Microsoft.CSharp to System.Xml.Xsl), and various tools (including a command-line debugger).

With the .NET Framework SDK, you can develop and deploy desktop applications, Web sites, or Web services. Although the .NET Framework SDK will work on Windows NT 4.0, ASP .NET requires IIS 5.0 or greater, so you must have Windows 2000, XP Professional, or .NET Server to create Web pages and Web services.

Using the C# Compiler

Once you've downloaded and installed the .NET Framework SDK, you can open a command prompt (cmd.exe) and develop your first C# program. Use your favorite text editor to create a file called Hello.cs. If you don't have a favorite text editor, try Notepad. You can create the file with the command notepad Hello.cs. Type the following into the new file and save it:


using System;
public class Hello 
{
  public static void Main()
  {
    // create a list of strings
    string[] words = {"hello", "goodbye"};
    // iterate over the list
    for (int i = 0; i <= words.Length; i++)
    {
      Console.WriteLine(words[i]);
    }
  }
}

To compile this program, use the command csc Hello.cs. This creates the executable Hello.exe:


C:\home>csc Hello.cs
Microsoft (R) Visual C# .NET Compiler 
version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. 
All rights reserved.

Running a Program

To run the program, type the command Hello, which invokes the executable:


C:\home>Hello
hello
goodbye

Unhandled Exception: 
System.IndexOutOfRangeException: Index was outside
the bounds of the array.
   at Hello.Main()

Depending on which components of Visual Studio .NET and/or the .NET Framework you chose to install, you might see a dialog box when this error occurs. Although the dialog offers to let you run a debugger, please choose "No" for now, which will display the error message and exit.

Enable Debugging

Comment on this articleWhat do you think of C# and .NET?
Post your comments

Bah! What's that error there? In a big program, there'd be no way of knowing, since you may have no idea which line number caused the error. To solve this problem, compile Hello.cs with the /debug switch and run it again. The /debug switch adds a little extra baggage in the form of a .pdb file, which contains debugging information:


C:\home>csc /debug Hello.cs
Microsoft (R) Visual C# .NET Compiler 
version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. 
All rights reserved.


C:\home>Hello
hello
goodbye

Unhandled Exception: 
System.IndexOutOfRangeException: Index was outside 
the bounds of the array.
   at Hello.Main() in C:\home\Hello.cs:line 11

That's a lot more helpful; at least you know the line number. Here's a look at that line:


       Console.WriteLine(words[i]);

Pages: 1, 2

Next Pagearrow