Channels ▼

C# Strikes a Chord

C# Strikes a Chord

C# Strikes a Chord

Microsoft's New Executable Environment

Probably the biggest change regarding the new Microsft.NET framework is the development of a brand new executable environment for all Microsoft compilers and scripting languages. It's called the Common Language Runtime (CLR), and on this point I have to be careful. I can't call it an interpreter or a virtual machine or the output p-code nor bytecode — heaven forbid! The output of all Visual Studio.Net languages will be either a Common Intermediate Language (CIL) file or an .EXE; but not your ordinary run of the mill .EXE. The .EXE now includes more metadata describing data and methods, creator and security permissions inlcuding an optional PKI signature, location of references to objects and exception handling tables. In turn, the CLR environment can provide both managed and unmanaged code execution. Unmanaged code execution is used to interface to COM, COM+, Win32, plus your own existing .DLL components and runs code after setup with the same performance and security they had before. Managed code execution provides the following additional services:

  • Better debugging and profiling
  • Automatic lifetime management of components and modules including
    • Garbage collection and memory management
    • Type safe method invocations and other specified exception checking
    • Thread pooling and asynchronous message handling for scalability
    • Side-by-side versions for managing shared components

  • Enhanced security of execution
    • Evidence based security knows code origin and publisher
    • Matches users permissions with codes profiles
    • Optionally may invoke PKI signature for validation
  • Better exception testing and handling

Microsoft hopes to address three pernicious problems. 1) rid itself of the security leaks presented by scripting, macro languages and its own passive, I-know-who-did-this enforcement ActiveX security procedures (think ILOVEYOU, Melissa Virus, and rogue ActiveX respectively); 2) eliminate .DLL Hell with side-by-side versioning; and 3) reduce the embarrassment of e-Registry dysfunction with each executable now being responsible for permissions and runtime attribute setting.

The new .NET Framework applies not to just C# but also all the other Microsoft compilers and scripting languages. However, C# is not only the key development language for the framework but also its guinea pig. According to Microsoft officials, C# is often the first language, even before VC++, used to test out some of the new CLR features. Also the new CLR executable environment goes well beyond the Java Virtual Machine. It is especially innovative in its ability to support existing COM and other components while interfacing to a wide range of languages (Microsoft has demoed COBOL, Component Pascal, Eiffel, Haskell, ML and Scheme running and taking advantage of the new CLR executable environment). Nonetheless Microsoft fails to give credit where credit is due. The JVM with its garbage collection, security, and runtime exception checking as well as various modes of compilation and execution proved that enterprise-caliber, cross-platform, managed code for executables is possible. Without the JVM, CIL and CLR would have been a very tough sell in Redmond.

The Software Libraries

Every language comes with a software library, so what is important about the Java and C# libraries? To begin with, they are very large, very comprehensive, and open (in Java's case). Microsoft has yet to decide what it will do about its .NET Base Classes, many of which have been written in C#. In fact, C# already has been used to produce millions of lines of for-production code. Some of these Base Class groupings include APIs for the following:

  • Base Data Types

    • byte, Int16, Decimal, DateTime and Object form of primitives
    • ToString/FromString/Convert conversion routines
    • Format/Parse routines

  • Strings

    • Comparison, transformation, and creation methods
    • Formatting strings with user control

  • Regular Expressions

  • Collection Classes

    • IList for indexed series, IDictionary for key-value pairs
    • Hashtable, Stack, Queue, ArrayList, etc

  • Globalization Text-encodings, Unicode

  • IO

    • Stream readers/writers
    • Binary readers/writers

  • Serialization

  • Cryptography

It is in the software libraries where C# departs most strongly from the Java model. One of Java's strengths is that it makes available on all platforms a huge, open, comprehensive set of routines for all aspects of computing. Even better, over the past five years the library has become more reliable and faster. C#, with its dedication to interoperate with other languages through the .NET Framework and CLR, takes a different approach. It, along with XML SOAP and SCL, is designed to make the huge COM, COM+ and Win32 APIs more available to all languages across all platforms. The direction is right — how reliable and fast it will be is still to be determined.

C# Innovations

C# brings its own unique innovations to the table. There are a number of new C# goodies like:

@"\\Print this\\n!"
verbatim strings which allow newlines, tabs and other whitespace.
case EDIT: doEdit(); break;
Strings can be case target in switch statements.
Indexers allow users to create array-like access to their multi-valued classes.
goto GoToIsBetter;
the goto is BACK. = "JBS";
like indexers, properties simplify access to a class' private data.
public static int x=1, y, z; is equivalent to: public static int x=1; public static int y; public static int z;

Destructors are restored making class instance clean up easier.

Namespace ERPFeatures{ ... }
namespaces allow more precise control of class visibility.
#define DEBUGON
preprocessor statements are back with neat behaviors.

This is only a sampling of some distinguishing features in C#. But perhaps the key innovations in C# are geared towards making the language more interoperable. First, primitive types have been unified into the C# object model so that special wrapper classes like Java's Integer for int or Byte for byte do not have to be declared. In addition, the mechanism for creating your own types is open, so interfacing with languages that have unique base types is simplified. Second, by making parameter passing much richer with ref, in, out, and params parameter-passing keywords, interfacing to other APIs, components and languages has been considerably eased. Finally, by adding attributes that allow passing specific info in metadata to the .NET Common Language Runtime, programmers can take advantage of being able to trigger specific runtime behavior depending on the attributes passed through to .NET executables. But this information can be used in any stage of the runtime process — debugging, install, JIT compile, loading and/or execution. In short, C# provides for very powerful interaction with the .NET executables, whatever language was originally used.

Next: Blending Old & New Ideas

1 | 2 | 3 | 4 | 5 | 6

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.