1. The Philosophy of .NET
Microsofts .NET platform (and the related C# programming language) were formally introduced circa 2002 and have quickly become a mainstay of modern-day software development. As mentioned in the books introduction, the goal of this text is twofold. The first order of business is to provide you with a deep and detailed examination of the syntax and semantics of C#. The second (equally important) order of business is to illustrate the use of numerous .NET APIs, including database access with ADO.NET and the Entity Framework (EF) , user interfaces with Windows Presentation Foundation (WPF) , service-oriented applications with Windows Communication Foundation (WCF) , and web service and web site development using ASP.NET MVC. The last part of this book covers the newest member of the .NET family, .NET Core, which is the cross-platform version of the .NET platform. As they say, the journey of a thousand miles begins with a single step; and with this I welcome you to Chapter .
The point of this first chapter is to lay the conceptual groundwork for the remainder of the book. Here you will find a high-level discussion of a number of .NET-related topics such as assemblies, the Common Intermediate Language (CIL) , and just-in-time (JIT) compilation. In addition to previewing some keywords of the C# programming language, you will also come to understand the relationship between various aspects of the .NET Framework, such as the Common Language Runtime (CLR), the Common Type System (CTS), and the Common Language Specification (CLS).
This chapter also provides you with a survey of the functionality supplied by the .NET base class libraries, sometimes abbreviated as BCLs. Here, you will get an overview of the language-agnostic and platform-independent nature of the .NET platform. As you would hope, many of these topics are explored in further detail throughout the remainder of this text.
An Initial Look at the .NET Platform
Before Microsoft released the C# language and .NET platform, software developers who created applications for the Windows family of operating system frequently made use of the COM programming model . COM (which stands for the Component Object Model ) allowed individuals to build libraries of code that could be shared across diverse programming languages. For example, a C++ programmer could build a COM library that could be used by a Visual Basic developer. The language-independent nature of COM was certainly useful; however, COM was plagued by a complicated infrastructure and a fragile deployment model and was possible only on the Windows operating system.
Despite the complexity and limitations of COM, countless applications have been successful created with this architecture. However, nowadays, the majority of applications created for the Windows family of operating systems are not created with COM. Rather, desktop applications, web sites, OS services, and libraries of reusable data access/business logic are created using the .NET platform.
Some Key Benefits of the .NET Platform
As mentioned, C# and the .NET platform were first introduced to the world in 2002 and were intended to offer a much more powerful, more flexible, and simpler programming model than COM. As you will see during the remainder of this book, the .NET Framework is a software platform for building systems on the Windows family of operating systems, as well as on numerous non-Microsoft operating systems such as macOS, iOS, Android, and various Unix/Linux distributions. To set the stage, here is a quick rundown of some core features provided courtesy of .NET:
Interoperability with existing code : This is (of course) a good thing. Existing COM software can commingle (i.e., interop) with newer .NET software, and vice versa. As of .NET 4.0 onward, interoperability has been further simplified with the addition of the dynamic keyword (covered in Chapter ).
Support for numerous programming languages : .NET applications can be created using any number of programming languages (C#, Visual Basic, F#, and so on).
A common runtime engine shared by all .NET-aware languages : One aspect of this engine is a well-defined set of types that each .NET-aware language understands.
Language integration: .NET supports cross-language inheritance, cross-language exception handling, and cross-language debugging of code. For example, you can define a base class in C# and extend this type in Visual Basic.
A comprehensive base class library : This library provides thousands of predefined types that allow you to build code libraries, simple terminal applications, graphical desktop applications, and enterprise-level web sites.
A simplified deployment model : Unlike COM, .NET libraries are not registered into the system registry. Furthermore, the .NET platform allows multiple versions of the same *.dll to exist in harmony on a single machine.
You will see each of these topics (and many more) examined in the chapters to come.
Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS)
Now that you know some of the major benefits provided by .NET, lets preview three key (and interrelated) topics that make it all possible: the CLR, CTS, and CLS. From a programmers point of view, .NET can be understood as a runtime environment and a comprehensive base class library. The runtime layer is properly referred to as the Common Language Runtime , or CLR . The primary role of the CLR is to locate, load, and manage .NET objects on your behalf. The CLR also takes care of a number of low-level details such as memory management, application hosting, coordinating threads, and performing basic security checks (among other low-level details).
Another building block of the .NET platform is the Common Type System , or CTS . The CTS specification fully describes all possible data types and all programming constructs supported by the runtime, specifies how these entities can interact with each other, and details how they are represented in the .NET metadata format (more information on metadata later in this chapter; see Chapter for complete details).
Understand that a given .NET-aware language might not support every feature defined by the CTS. The Common Language Specification , or CLS , is a related specification that defines a subset of common types and programming constructs that all .NET programming languages can agree on. Thus, if you build .NET types that expose only CLS-compliant features, you can rest assured that all .NET-aware languages can consume them. Conversely, if you make use of a data type or programming construct that is outside of the bounds of the CLS, you cannot guarantee that every .NET programming language can interact with your .NET code library. Thankfully, as you will see later in this chapter, it is simple to tell your C# compiler to check all of your code for CLS compliance.
The Role of the Base Class Libraries
In addition to the CLR, CTS, and CLS specifications, the .NET platform provides a base class library that is available to all .NET programming languages. Not only does this base class library encapsulate various primitives such as threads, file input/output (I/O), graphical rendering systems, and interaction with various external hardware devices, but it also provides support for a number of services required by most real-world applications.