- Get the first "hands on" look at C#
- Copyright
- Chapter 1 Introduction to C#
- Why Another Programming Language?
- Summary
Why Another Programming Language?
A good question that has to be answered is why you should learn another programming language when you are already doing enterprise development in C++ or Visual Basic. The marketing-type answer is that "C# is intended to be the premier language for writing .NET applications in the enterprise computing space." This chapter is about backing up that claim with arguments, and showcasing a slew of C#'s features. This chapter is about whetting your appetite.
The programming language C# derives from C and C++; however, it is modern, simple, entirely object-oriented, and type-safe. If you are a C/C++ programmer, your learning curve will be flat. Many C# statements are directly borrowed from your favorite language, including expressions and operators. If you don't look too closely at first, a C# program looks like a C++ program.
An important point about C# is that it is a modern programming language. It simplifies and modernizes C++ in the areas of classes, namespaces, method overloading, and exception handling. Much of the complexity of C++ was removed from C# to make it easier to use and less error prone.
Contributing to the ease of use is the elimination of certain features of C++: no more macros, no templates, and no multiple inheritance. The aforementioned features create more problems than they provide benefitespecially for enterprise developers.
New features for added convenience are strict type safety, versioning, garbage collection, and many more. All these features are targeted at developing component-oriented software. Although you don't have the sheer power of C++, you become more productive faster.
Before I get ahead of myself and present too many features, I want to stop and present the various elements of C# based on key points in the following sections:
- Simple
- Modern
- Object-oriented
- Type-safe
- Versionable
- Compatible
- Flexible
Simple
One thing you definitely wouldn't attribute to C++ is that learning it is simple. This is not so with C#. The foremost goal for this programming language was simplicity. Many featuresor the lack thereofcontribute to the overall simplicity of C#.
Pointers are a prominent feature that is missing in C#. By default, you are working with managed code, where unsafe operations, such as direct memory manipulation, are not allowed. I don't think any C++ programmer can claim never to have accessed memory that didn't belong to him via a pointer.
Closely related to the pointer "drama" is operator "madness." In C++, you have ::, ., and -> operators that are used for namespaces, members, and references. For a beginner, operators make for yet another hard day of learning. C# does away with the different operators in favor of a single one: the . (the "dot"). All that a programmer now has to understand is the notion of nested names.
You no longer have to remember cryptic types that originate from different processor architecturesincluding varying ranges of integer types. C# does away with them by providing a unified type system. This type system enables you to view every type as an object, be it a primitive type or a full-blown class. In contrast to other programming languages, treating a simple type as an object does not come with a performance penalty because of a mechanism called boxing and unboxing. Boxing and unboxing is explained later in detail, but basically, this technique provides object access to simple types only when requested.
At first, seasoned programmers might not like it, but integer and Boolean data types are now finally two entirely different types. That means a mistaken assignment in an if statement is now flagged as an error by the compiler because it takes a Boolean result only. No more comparison-versus-assignment errors!
C# also gets rid of redundancies that crept into C++ over the years. Such redundancies include, for example, const versus #define, different character types, and soon. Commonly used forms are available in C#, whereas the redundant forms were eliminated from the language.
Modern
The effort you put into learning C# is a great investment because C# was designed to be the premier language for writing .NET applications. You will find many features that you had to implement yourself in C++, or that were simply unavailable, are part of the base C# language implementation.
The financial types are a welcome addition for an enterprise-level programming language. You get a new decimal data type that is targeted at monetary calculations. If you are not fond of the provided simple types, you can easily create new ones specifically crafted for your application.
I have already mentioned that pointers are no longer part of your programming weaponry. You won't be too surprised then that the entire memory management is no longer your dutythe Common Language Runtime (CLR) provides a garbage collector that is responsible for memory management in your C# programs. Because memory and your application are managed, it is imperative that type safety be enforced to guarantee application stability.
It is not exactly news to C++ programmers, but exception handling is a main feature of C#. The difference from C++, however, is that exception handling is cross-language (another feature of the runtime).Prior to C#, you had to deal with quirky HRESULTsthis is now over because of robust error handling that is based on exceptions.
Security is a top requirement for a modern application. C# won't leave you alone on this either: It provides metadata syntax for declaring capabilities and permissions for the underlying .NET security model. Metadata is a key concept of the CLR, and the next chapter deals with its implications in more depth.
Object-Oriented
You wouldn't expect a new language to not support object-oriented features, would you? C#, of course, supports all the key object-oriented concepts such as encapsulation, inheritance, and polymorphism. The entire C# class model is built on top of the CLR's Virtual Object System(VOS), which is described in the next chapter. The object model is part of the infrastructure, and is no longer part of the programming language.
One thing you will notice right from the start is that there are no more global functions, variables, or constants. Everything must be encapsulated inside a class, either as an instance member (accessible via an instance of a classan object) or a static member (via the type). This makes your C# code more readable and also helps to reduce potential naming conflicts.
The methods you can define on classes are, by default, non virtual (they cannot be overridden by deriving classes). The main point of this is that another source of errors disappearsthe accidental overriding of methods. For a method to be able to be overridden, it must have the explicit virtual modifier. This behavior not only reduces the size of the virtual function table, but also guarantees correct versioning behavior.
When you are used to programming classes in C++, you know that you can set different access levels for class members by using access modifiers. C# also supports the private, protected, and public access modifiers, and also adds a fourth one: internal. Details about these access modifiers are presented in chapter 5, "Classes."
How many of you have ever created a class that derives from multiple base classes? (ATL programmers, your vote doesn't count!) In most cases, you need to derive from only one class. Multiple base classes usually add more problems than they solve. That is why C# allows only one base class. If you feel the need for multiple inheritance, you can implement interfaces.
A question that might come up is how to emulate function pointers when there are no pointers in C#. The answer to this question is delegates, which provide the underpinnings for the CLR's event model. Again, I have to put off a full explanation until chapter 5.
Type-Safe
Once again, I have to pick on pointers as an example. When you had a pointer in C++, you were free to cast it to any type, including doing rather idiotic things such as casting an int* (int pointer) to a double* (double pointer). As long as memory backed that operation, it kind of "worked." This is not the kind of type safety you would envision for an enterprise-level programming language.
Because of the outlined problems, C# implements strictest type safety to protect itself and the garbage collector. Therefore, you must abide by a few rules in C# with regard to variables:
- You cannot use uninitialized variables. For member variables of an object, the compiler takes care of zeroing them. For local variables, you are incharge. However, if you use an uninitialized variable, the compiler will tell you so. The advantage is that you get rid of those errors when using an uninitialized variable to compute a result and you don't know how these funny results are produced.
- C# does away with unsafe casts. You cannot cast from an integer to a reference type (object, for example), and when you downcast, C# verifies that this cast is okay. (That is, that the derived object is really derived from the class to which you are down casting it.)
- Bounds checking is part of C#. It is no longer possible to use that "extra" array element n, when the array actually has n-1 elements. This makes it impossible to overwrite unallocated memory.
- Arithmetic operations could overflow the range of the result data type. C# allows you to check for overflow in such operations on either an application level or a statement level. With overflow checking enabled, an exception is thrown when an overflow happens.
- Reference parameters that are passed in C# are type-safe.
Versionable
Over the past few years, almost every programmer has had to deal at least once with what has become known as "DLL Hell." The problem stems from the fact that multiple applications install different versions of the same DLL to the computer. Sometimes, older applications happily work with the newer version of the DLL; however, most of the time, they break. Versioning is a real pain today.
As you will see in chapter 8, "Writing Components in C#," the versioning support for applications you write is provided by the CLR. C# does its best to support this versioning. Although C# itself cannot guarantee correct versioning, it can ensure that versioning is possible for the programmer. With this support in place, a developer can guarantee that as his class library evolves, it will retain binary compatibility with existing client applications.
Compatible
C# does not live in a closed world. It allows you access to different APIs, with the foremost being the .NET Common Language Specification (CLS). The CLS defines a standard for interoperation between languages that adhere to this standard. To enforce CLS compliance, the compiler of C# checks that all publicly exported entities comply, and raises an error if they do not.
Of course, you also want to be able to access your older COM objects. The CLR provides transparent access to COM. Integration with legacy code is presented in chapter 10, "Interoperating with Unmanaged Code."
OLE Automation is a special kind of animal. Anyone who ever created an OLE Automation project in C++ will have come to love the various Automation data types. The good news is that C# supports them, without bothering you with details.
Finally, C# enables you to inter operate with C-style APIs. Any entry point in a DLLgiven its C-stylednessis accessible from your applications. This feature for accessing native APIs is called Platform Invocation Services (PInvoke), and chapter 10 shows a few examples of inter operating with C APIs.
Flexible
The last paragraph of the previous section might have raised an alert with C programmers. You might ask, "Aren't there APIs to which I have to pass a pointer?" You are right. There are not only a few such APIs, but quite a large number (a small understatement). This access to native WIN32 code sometimes makes using unsafe classic pointers mandatory (although some of it can be handled by the support of COM and PInvoke).
Although the default for C# code is safe mode, you can declare certain classes or only methods of classes to be unsafe. This declaration enables you to use pointers, structs, and statically allocated arrays. Both safe code and unsafe code run in the managed space, which implies that no marshaling is incurred when calling unsafe code from safe code.
What are the implications of dealing with your own memory in unsafemode? Well, the garbage collector, of course, may not touch your memory locations and move them just as it does for managed code. Unsafe variables are pinned into the memory block managed by the garbage collector.