ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


What Is C#

by Jesse Liberty, author of Programming C#,
4th Edition
and Visual C# 2005: A Developer's Notebook
10/03/2005
Help Me Help You
C#
C# is a type-safe, component-based, high-performance language that is designed for the Microsoft .NET framework. C# 2.0 is the new generation of C#, upgraded with Generics and other advanced features and fully integrated into .NET 2.0 and Visual Studio 2005. If you are developing Windows or web applications or web services for the .NET platform, C# is in many ways the language of choice.

This article discusses the following:

  1. The .NET Platform
  2. The C# Language
  3. Coding with C#

While Microsoft and its competitors are always innovating and creating new ways to approach programming, about every ten years or so an approach so revolutionary comes along that it totally changes not only the way we program, but the way we think about programming.

In the early 1980s, the new technologies were Unix and a powerful new language called C. The early '90s brought Windows and C++. In 2000, .NET and C# were the next wave. Each of these developments represented a sea change in the way we approached programming.

.NET and C# have matured greatly in five years, and the new versions, C# 2.0 and .NET 2005, represent a significant increase in productivity and a good opportunity to examine C# closely to understand what it is. This is especially important because C# is arguably the primary language for programming in .NET (alongside Visual Basic 2005) and Microsoft has "bet the company" on .NET.

When a company of Microsoft's size and influence spends billions of dollars a year on R&D, and reorganizes their entire corporate structure to support a new platform, programmers are required to take notice. The term ".NET" has been overmarketed by Microsoft, but the part we care about amounts to a new operating system layered on top of the existing Windows platform (allowing for backward compatibility while creating a new object-oriented operating system) and the development tools created to facilitate development for that platform.

The nasty little secret about .NET is that Microsoft really created only one language for the .NET platform. This language is called Microsoft Intermediate Language (MSIL). They then created two coatings for MSIL. One looks a lot like C++ or Java, and they named it C#; the other looks a lot like VB6 and they named it VB 2005. Both C# and VB 2005 produce MSIL, and it is MSIL that runs on the .NET platform.

C# 2.0 comes with updated tools and a powerful new development environment. It is the crowning achievement of Microsoft's R&D investment. And it is wicked cool.

The goal of C# 2.0 is to provide a mature, simple, safe, modern, object-oriented, internet-centric, high-performance language for .NET development. In much the way that you can see in young children the features and personalities of their parents and grand­parents, you can easily see in C# the influence of Java, C++, Visual Basic (VB), and other languages, but you can also see the lessons learned since C# was first introduced.

The .NET Platform

The .NET platform is a development framework that provides a new application programming interface (API) to the services and APIs of classic Windows operating systems while bringing together a number of disparate technologies that emerged from Microsoft during the late 1990s. This includes COM+ component services, a commitment to XML and object-oriented design, support for standardized protocols such as SOAP, WSDL, and UDDI, and a focus on the internet.

Since all .NET Common Language Specification compliant languages (like C#) produce MSIL, you can inherit from classes, catch exceptions, and take advantage of polymorphism across different .NET languages. The .NET Framework makes this possible with a specification called the Common Type System (CTS) that all .NET components must obey. For example, everything in .NET is an object of a specific class that derives from the root class called System.Object. The CTS supports the general concepts of classes, interfaces, and delegates (which support callbacks).

The CLR

The most important component of the .NET Framework is the Common Language Runtime (CLR), which provides the environment in which programs are executed. The CLR includes a virtual machine that creates, manages, secures and cleans up after objects. The CLR is a set of framework classes that include a layer for data (ADO.NET) and for XML. On top of all this, the CLR creates a layer for three types of applications: Windows desktop applications, web Applications and web services (see future article, "What Are Web Services?").

The classes that Microsoft provides to support these applications and to provide the "plumbing" for network operations, threading, database interaction, creating the user interface and so forth, are called the Framework Class Library (FCL). The .NET FCL is one of the largest class libraries in history; it provides an object model for all the functionality of the .NET platform. With more than 4,000 classes, the FCL facilitates rapid development of both desktop and web applications.

How to Keep Your Boss from Sinking Your Project

Essential Reading

How to Keep Your Boss from Sinking Your Project
By Andrew Stellman, Jennifer Greene

Like it or not, your project needs management. Yet few good software projects can survive bad management. If you're a programmer on a high-visibility project, this PDF offers five principle guidelines for managing upward that will help you help your boss make the right decisions about setting project expectations, working with users and stakeholders, putting the project on the right track and keeping it there. The PDF also covers what problems cause projects to fail and how to fix them, and what you can do to keep your software project from running into trouble.


Read Online--Safari
Search this book on Safari:
 

Code Fragments only

The FCL

The lowest-level FCL classes provide I/O support, strings, security, network communications, thread management, reflection and collection classes. One level higher is a set of classes to support data management and iterating with databases, as well as XML manipulation, searching, translations and so forth. The highest-level classes provide support for creating applications and user interfaces. Web applications are built with Web Forms and Web Controls, while Windows applications are built using Windows Forms and Windows Controls. The two models are strikingly similar, though not identical.

Compilation and the MSIL

As noted earlier, .NET Windows and web programs are not compiled into executable files, they are compiled into Assemblies that consist of Microsoft Intermediate Language (MSIL) instructions. The IL is saved in a file on disk. When you run your program, the IL is compiled again, using the Just In Time (JIT) compiler (a process often called JITing ). The result is machine code, executed by the machine's processor.

The standard JIT compiler runs on demand. When a method is called, the JIT compiler analyzes the IL and produces highly efficient machine code, which runs very fast. As the application runs, compilation happens only as needed, and once JIT-compiled, the code is cached for future use. As .NET applications run, they tend to become faster and faster, as the already-compiled code is reused.

The C# Language

The C# language is disarmingly simple, but highly expressive when it comes to implementing modern programming concepts. C# includes all the support for structured, component-based, object-oriented programming that one might expect from a language that is the virtual child of both C++ and Java. With the release of C# 2.0, many of the most important missing ingredients in the original C#, such as generics and anonymous methods, have been added.

The heart of any object-oriented language is its support for defining and working with classes. Classes define new types, allowing you to extend the language to better model the problem you are trying to solve. C# contains keywords for declaring new classes and their methods and properties, and for implementing encapsulation, inheritance, and polymorphism, the three pillars of object-oriented programming.

In C#, everything pertaining to a class declaration is found in the declaration itself. C# class definitions do not require separate header files or Interface Definition Language (IDL) files. You can document your C# programs with special XML comments that produce an XML file that in turn can be transformed into HTML or Microsoft Help files.

C# supports interfaces, a means of making a contract with a class for services that the interface stipulates. In C#, a class can inherit from only a single parent, but a class can implement multiple interfaces. When it implements an interface, a C# class in effect promises to provide the functionality the interface specifies.

C# provides full support for indirect method calls through the use of delegates, which are themselves first-class members of the language. This feature (especially when combined with "anonymous delegates") allows for an elegant event-handling solution as well as for the creation of easy-to-use and easy-to-maintain callback methods.

C# also provides component-oriented features such as properties, and declarative constructs such as attributes. Component-oriented programming is supported by the storage of metadata with the code for the class. The metadata describes the class, including its methods and properties, as well as its security needs and other attributes, such as whether it can be serialized; the code contains the logic necessary to carry out its functions. A compiled class is thus a self-contained unit. Therefore, a hosting environment that knows how to read the metadata needs no other information to make use of the class. Using C# and the CLR, it is possible to add custom metadata to a class by creating custom attributes. Likewise, it is possible to read class metadata using CLR types that support reflection.

Probably the biggest breakthough in C# if you are coming from a more traditional object-oriented environment like C++ is the presence of Garbage Collection. Most C++ programmers find that development time is swamped by debugging and maintenance time, and most of that is taken up with finding and plugging memory leaks. With C# there are no memory leaks because the garbage collector finds objects that are no longer referenced and returns their memory to the heap for reuse.

A final note about C# is that it also provides support for directly accessing memory using C++ style pointers and keywords for bracketing such operations as unsafe, and for warning the CLR garbage collector not to collect objects referenced by pointers until they are released. Because of the architecture of the CLR, the operating system can be potentially any variety of Unix or another operating system altogether.

Coding with C#

There are many different types of things you might create with C# (custom controls, libraries, and so on) but the four types of applications are Console applications (that are primarily used for learning the language), Windows desktop applications, Web applications and Web Services. Each of these can be created using Notepad and the command-line compiler, but no serious C# developer ever does that for long. Visual Studio 2005 provides extensive support for drag-and-drop development, for integrated debugging and for testing and deployment.

For articles that will get you coding with C#, please see this excerpt from my book Learning C#, and for more on some of the advanced features in C# 2.0, take a look at this chapter from the fourth edition of Programming C#. Finally, for a glimpse at a commercial product developed with C#, please see my article "Building a Complex Custom Control: Rolodex".

Jesse Liberty is a senior program manager for Microsoft Silverlight where he is responsible for the creation of tutorials, videos and other content to facilitate the learning and use of Silverlight. Jesse is well known in the industry in part because of his many bestselling books, including O'Reilly Media's Programming .NET 3.5, Programming C# 3.0, Learning ASP.NET with AJAX and the soon to be published Programming Silverlight.


Return to ONDotnet.com.

Copyright © 2009 O'Reilly Media, Inc.