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):
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.
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).
|
Related Reading C# Essentials |
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.
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.
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.
|
| |
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]);
|
The .NET Framework SDK utilities include a debugger, and if you learn to
use it, your life as a .NET programmer will be much simpler. Let's use the
debugger (cordbg.exe) to examine this problem. First, start
the program under control of the debugger. You are greeted by the
(cordbg) prompt. The command run Hello.exe is
invoked automatically. Ignore that warning about mscorlib.dll; this just tells you that you can't debug the core library that contains many
important Framework Class Library types:
C:\home>cordbg Hello.exe
Microsoft (R) Common Language Runtime Test
Debugger Shell Version 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001.
All rights reserved.
(cordbg) run Hello.exe
Process 1132/0x46c created.
Warning: couldn't load symbols for
c:\windows\microsoft.net\framework\v1.0.3705\mscorlib.dll
[thread 0xe10] Thread created.
007: string[] words = {"hello", "goodbye"};
(cordbg)
The first thing you'll do is set a breakpoint on the offending line with the
b command, and start the program with go. This
will stop execution in the first iteration of the loop and prompt you for
the next action:
007: string[] words = {"hello", "goodbye"};
(cordbg) b 11
Breakpoint #1 has bound to C:\home\Hello.exe.
#1 C:\home\Hello.cs:11 Main+0x1d(il) [active]
(cordbg) go
break at #1 C:\home\Hello.cs:11
Main+0x1d(il) [active]
011: Console.WriteLine(words[i]);
(cordbg)
At this point, the variable i should be set to zero, and
words[i] should be "hello". You can use the
print command to test this out:
(cordbg) print i
i=0x00000000
(cordbg) print words[0]
words[0]=(0x00c41844) "hello"
You can repeat this for the next iteration of the loop:
(cordbg) go
hello
break at #1 C:\home\Hello.cs:11
Main+0x1d(il) [active]
011: Console.WriteLine(words[i]);
(cordbg) print i
i=0x00000001
(cordbg) print words[1]
words[1]=(0x00c41860) "goodbye"
|
Related Reading C# Essentials |
Since there are two elements in the array, that should have been the last
iteration of the loop, right? But when you type go for a
third time, the program enters the body of the loop again:
(cordbg) go
goodbye
break at #1 C:\home\Hello.cs:11
Main+0x1d(il) [active]
011: Console.WriteLine(words[i]);
And what's worse, the current value of i is outside the
boundaries of the array (the array goes from 0 to 1):
(cordbg) print i
i=0x00000002
(cordbg) print words
words=(0x00c41880) array with dims=[2]
words[0] = (0x00c41844) "hello"
words[1] = (0x00c41860) "goodbye"
Use the show command to list five lines before and five lines
after the current line of code:
(cordbg) show
006: // create a list of strings
007: string[] words = {"hello", "goodbye"};
008: // iterate over the list
009: for (int i = 0; i <= words.Length; i++)
010: {
011:* Console.WriteLine(words[i]);
012: }
013: }
014: }
Take a look at line 9, where the loop is
controlled. It seems that I've introduced an off-by-one error by using
<= instead of <. Problem solved! Quit the
debugger with the quit command and open up your text editor
again to correct this error. The following example shows the corrected
program:
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]);
}
}
}
And here is the output of running the program--no error this time!
C:\home>Hello
hello
goodbye
Notepad is useful, but it doesn't have a huge set of features. For some people, this is just right; others may need something more featureful. Here is a list of C# editors I have experimented with:
vi-compatible editor with many extra features.
Grab csharp.vim
to get C# syntax support.
For more C# editors, see csharpindex.com's list of text editors.
If you're not using a Windows platform but you'd still like to play around with C#, check out Mono or dotGNU, two projects that provide ECMA-compliant C# compilers and partial implementations of the .NET Framework Class Library. At the time of this writing, the C# compilers are still in their early stages, but a determined user shouldn't have any trouble using the compilers to develop and test simple C# programs. Also, Microsoft has announced a shared-source implementation of .NET that will include a C# compiler.
Brian Jepson is an O'Reilly editor, programmer, and co-author of Mac OS X Panther for Unix Geeks and Learning Unix for Mac OS X Panther. He's also a volunteer system administrator and all-around geek for AS220, a non-profit arts center in Providence, Rhode Island. AS220 gives Rhode Island artists uncensored and unjuried forums for their work. These forums include galleries, performance space, and publications. Brian sees to it that technology, especially free software, supports that mission. You can follow Brian's blog here.
O'Reilly & Associates recently released (January 2002) C# Essentials, 2nd Edition.
Sample Chapter 1, Introduction, is available free online.
You can also look at the Table of Contents, the Index, and the Full Description of the book.
For more information, or to order the book, click here.
Return to the .NET DevCenter.
Copyright © 2009 O'Reilly Media, Inc.