WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

What Is C#
Pages: 1, 2

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.