Introduction

 

Microsoft Corporation s newest compiler, C# (pronounced C sharp ), is now with us, and may become one of the most celebrated compilers of the 21st century ” if we can learn how to use its immense capabilities. Some have questioned the immediate value of this new compiler, since it was only a decade ago that C++ emerged from the computational think tanks of the world. What s wrong with continuing to use C++ ? they ask.

The answer is this: C++ will continue to serve some of us comfortably in the near term , but there are features built into C# that make life easier when we program for the Internet and other remote processes. We must become more attuned to the needs of the Internet.

In some cases, however, C# adds a degree of complexity to the compiler that was not there before. For example, Charles Petzold mentions in his book Programming Microsoft Windows with C# that there are half a dozen forms of the StreamWriter constructor that use file names , three forms that use a Stream object, and several other forms for special uses. Some of the common C# commands have as many as 16 variations. Petzold also discusses encoding schemes like Unicode, UTF7, UTF8, and finally, ASCII (thank goodness). A bead of perspiration broke out on my forehead when I read that. How could I possibly remember all the variations of the simple C commands that I have learned to love over the years ?

Then I remembered how it was 15 years ago when the C language first evolved into the C++ language and program architecture became object-oriented. C++ was not a superfluous change to a basic C script ” the change forced us to become more focused programmers since all the C++ procedures were narrowly defined and all the variables were hard-typed so we could not misuse them like we did in the old C days. The new methods of creating source code were quite a change for the, shall we say, experienced programmer. There were so many new ways to make mistakes in the code we were attempting to write. In retrospect, however, object-oriented programming was a godsend.

Along with the increased complexity of the compilers, more attention was given by the compiler manufacturers to helps that could lead the average programmer out of the quandary of overspecific compiler statements. You will be pleased to find that the new generation of helps in the Microsoft C# language outstrips any automated technical support you have encountered so far.

These helps are not just laying dormant in a multi-meg text document that accompanies the new C# compiler ” they are active bells , whistles, and gongs that warn the programmer when he has done something wrong in preparing source code or while combining multiple groups of source code into one integrated executable (an msi). It s like having your favorite computer science professor looking over your shoulder every time you write source code ” in real time no less! The new compilers have protected themselves from an onslaught of poor overall planning on the part of the programmer, poor sequencing of computer tasks , and poor statement choices (like the dredging up of old statements learned 20 years ago that are no longer in the repertoire ). If a person wants to learn to program in C#, then she had better learn the nuances of the C# language!

This is not to say that the nuances are that great. If you have the mindset of a computer programmer, then the C# nuances will be embedded in your brain in a few weeks, and the lingering memories of the old ways of creating computer task sequences with C or C++ will fade into the sunset.

Like every new language, however, the C# architects included multiple ways of accomplishing the same task within the C# language. This leads to confusion among the newly washed members of the programming community. For example, why is there more than one way in C# to open a file and read it? And the answer is: Because the old methods that have appeared in FORTRAN, COBOL, Pascal, ADA, Smalltalk, Java, and so forth since the 1960s all have their devotees in the programming community, and no compiler manufacturer wants to alienate them. Also, within a given compiler-creating architectural group there are wide differences of opinion on how basic computing tasks should be bundled and provided to the programmer ” hence, the redundancy continues.

Should the writers of a computer science handbook attempt to show all the methods of performing a standard programming task to programmers who are new to C#? One can certainly do this, of course, but the ensuing work may be three inches thick and printed in eight-point text. To avoid this lapse into redundancy and make the transition to C# easier, we decided on one way to perform each common software programming task and cast aside all others. We picked the best way to accomplish all the common programming tasks, and those best ways are in this book.

In some cases, code snippets were not forceful enough (or self-explanatory enough) to demonstrate how C# statements should be used in programs, so we undertook the task of creating a set of programs under the main title Keeping Track, a set of programs to oversee one s stocks/ bonds /funds in the financial market. Keeping Track (KT) is a collection of seven C# programs bundled under one entry point. The KT suite of programs is described more fully in Appendix A. The programs are yours to experiment with ” the companion files (available at www.wordware.com/unlockingcsharp) contain both the source code (8,300 lines) and the compiled code for KT. With some thought and experimentation you can elevate KT from a simple set of humdrum programs to a super set. Have at it!

As we wrote this book it became harder and harder to point the reader to particular snippets of code without hand-tagging every line, so we finally placed the demonstration source code and all the KT source code into Microsoft Word and gave each line a separate index number on the left edge of the page, like EP0039. With the help of a word processor like Word or Notepad and its search capability, the reader can locate all the referenced code in a flash. The code can be extracted and moved to new projects using standard copy and paste techniques.

One element of stress still remains: How can the reader look up C# code in an indexed listing when he doesn t know the name of the statement needed? (Remember how it was when you asked someone how to spell a word, and that person told you to look it up in the dictionary? How can you look up a word in the dictionary when you don t know how to spell it?) For example, if you have performed C programming in the past, then you can think of two commands to copy a file: CopyFile and FileCopy. Neither one of these commands is found in C#, which uses FileInfo followed by CopyTo. To help you resolve this dilemma, Appendix B provides a cross-reference to most of the common C and C++ commands and their new C# counterparts.

System Architectural Differences between C++ and C#

There are some nuances about the C# compiler (and more correctly, the .NET Framework in which the C# compiler plays a significant role) that should be mentioned from the outset. There are some features of the .NET Framework that will excite you and others that will disappoint you. Part of the disappointment is due to the fact that the Microsoft compiler group is anxious to move the world s computer programmers out of a climate where a given compiler produces machine code that executes properly in a narrowly defined set of computing machines, like PCs for example, but fails in other applications. They seek to create source code in an intermediate language that will service all known computing hardware formats.

To accomplish this universal source coding technique, Microsoft did exactly what the Java group (Sun Microsystems) did several years ago. Microsoft wrote an outstanding C# compiler that produces programs written in an intermediate language ( not machine code, as we have learned to expect).

How did Java pull off this radical departure from expected programming etiquette? They wrote cookies (also known as run-time language compilers) that were placed on each PC or Macintosh or Sun machine or IBM mainframe or European computer that took the intermediate code and converted it to machine code that the individual computing machine could understand. It sounds a little goofy, but it worked!

So we warn you in advance that all is not as it may seem in this Microsoft C# compiler! The source code that you generate will compile in the Microsoft-provided compiler, and each Microsoft program will produce an executable that is intermediate code and not machine code. You can execute each program with the usual PC methods. But what you don t know is this: A common run-time language is present on your PC as a giant cookie, and if you do not have that common run-time cookie on your PC then you cannot run the programs !

What all this means is this: If you write a program and give it to a friend across the street for execution on his PC, there is a good chance that the program will fail at run time. But all is not lost! The person to whom you gave the program can load this giant cookie on his PC by doing one of two things:

  • Get a copy of the Microsoft Prerequisites CD that is shipped with every copy of the C# compiler and place it in the CD reader. Use Explorer to open the .NET Framework subdirectory and run dotnetfx.exe (it s the only file in the subdirectory). The self-extracting process begins to place the 23 MB cookie at several locations in the Windows subdirectory. The extraction process takes about five minutes, and now the program is ready to be executed. You, of course, already have the .NET Framework cookie on your PC because it comes with the C# compiler.

  • A link to the latest version of dotnetfx.exe is available on the download page for this book. Navigate to www.wordware.com/unlockingcsharp and click the dotnetfx.exe link. When you get to the Microsoft page, click the Download button and follow the instructions.

Does this process seem a little obtuse to you? A 23 MB cookie? Well, it is. But Microsoft is moving quickly to place this giant cookie in every copy of every operating system they manufacture by the end of 2005. And, since most owners of Windows XP have agreed to allow continuous automatic updates of their operating system via the Internet, most of the Microsoft operating systems will include the results of dotnetfx.exe in the near future. Meanwhile, in the near future, you and your neighbor across the street are going to have to work something out so you can trade code easily.

By early 2006 every Microsoft operating system should have this problem resolved.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

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