Building blocks of .NET

In this article we view three key (and interrelated) topics that make it all possible: the CLR, CTS, and CLS.

Common Language Runtime

From a programmer’s 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).

Common Type System

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.

Common Language Specification

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, it is simple to tell your C# compiler to check all of your code for CLS compliance.

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.

The base class libraries define types that can be used to build any type of software application. For example, you can use ASP.NET to build web sites and REST services, WCF to build distributed systems, WPF to build desktop GUI applications, and so forth. As well, the base class libraries provide types to interact with XML documents, the directory and file system on a given computer, communicate with a relational databases (via ADO.NET), and so forth. From a high level, you can visualize the relationship between the CLR, CTS, CLS, and the base class library, as shown in image:

What's new in C#?

C# is a programming language whose core syntax looks very similar to the syntax of Java. However, calling C# a Java clone is inaccurate. In reality, both C# and Java are members of the C family of programming languages (e.g., C, Objective C, C++) and, therefore, share a similar syntax. The truth of the matter is that many of C#’s syntactic constructs are modeled after various aspects of Visual Basic (VB) and C++. For example, like VB, C# supports the notion of class properties (as opposed to traditional getter and setter methods) and optional parameters. Like C++, C# allows you to overload operators, as well as create structures, enumerations, and callback functions (via delegates). Moreover, as you work through this text, you will quickly see that C# supports a number of features traditionally found in various functional languages such as lambda expressions and anonymous types. Furthermore, with the advent of Language Integrated Query (LINQ), C# supports a number of constructs that make it quite unique in the programming landscape. Nevertheless, the bulk of C# is indeed influenced by C-based languages.

Because C# is a hybrid of numerous languages, the result is a product that is as syntactically clean (if not cleaner) as Java, is about as simple as VB, and provides just about as much power and flexibility as C++. Here is a partial list of core C# features that are found in all versions of the language:
  • No pointers required! C# programs typically have no need for direct pointer manipulation (although you are free to drop down to that level if absolutely necessary).
  • Automatic memory management through garbage collection. Given this, C# does not support a delete keyword.
  • Formal syntactic constructs for classes, interfaces, structures, enumerations, and delegates.
  • The C++-like ability to overload operators for a custom type, without the complexity.
  • Support for attribute-based programming. This brand of development allows you to annotate types and their members to further qualify their behavior. For example, if you mark a method with the Obsolete attribute, programmers will see your custom warning message print out if they attempt to make use of the decorated member.
With the release of .NET 2.0 (circa 2005), the C# programming language was updated to support numerous new bells and whistles, most notability the following:
  • The ability to build generic types and generic members. Using generics, you are able to build efficient and type-safe code that defines numerous placeholders specified at the time you interact with the generic item.
  • Support for anonymous methods, which allow you to supply an inline function anywhere a delegate type is required.
  • The ability to define a single type across multiple code files (or if necessary, as an inmemory representation) using the partial keyword,
    .NET 3.5 (released circa 2008) added even more functionality to the C# programming language, including the following features:
  • Support for strongly typed queries (e.g., LINQ) used to interact with various forms of data.
  • Support for anonymous types that allow you to model the structure of a type (rather than its behavior) on the fly in code.
  • The ability to extend the functionality of an existing type (without subclassing) using extension methods.
  • Inclusion of a lambda operator (=>), which even further simplifies working with .NET delegate types.
  • A new object initialization syntax, which allows you to set property values at the time of object creation.
.NET 4.0 (released in 2010) updated C# yet again with a handful of features.
  • Support for optional method parameters, as well as named method arguments.
  • Support for dynamic lookup of members at runtime via the dynamic keyword.this provides a unified approach to invoking members on the fly, regardless of which framework the member implemented (COM, IronRuby, IronPython, or via .NET reflection services).
  • Working with generic types is much more intuitive, given that you can easily map generic data to and from general System.Object collections via covariance and contravariance.
With the release of .NET 4.5, C# received a pair of new keywords (async and await), which greatly simplify multithreaded and asynchronous programming. If you have worked with previous versions of C#, you might recall that calling methods via secondary threads required a fair amount of cryptic code and the use of various .NET namespaces. Given that C# now supports language keywords that handle this complexity for you, the process of calling methods asynchronously is almost as easy as calling a method in a synchronous manner. This brings us to the current version of C# and .NET 4.6, which introduces a number of minor features that help streamline your codebase. You will see a number of details as you go through; however, here is a quick rundown of some of the new features found in C#:
  • Inline initialization for automatic properties as well as support for read-only automatic properties
  • Single-line method implementations using the C# lambda operator
  • Support of “static imports” to provide direct access to static members within a namespace
  • A null conditional operator, which helps check for null parameters in a method implementation
  • A new string formatting syntax termed string interpolation
  • The ability to filter exceptions using the new when keyword

Colourful console program in C#

Program


using System;

namespace csharp
{
    public class ConsoleApp
    {  
        public static void Main(string[] args)
        {
            Console.Title = "My Application";
            Console.ForegroundColor = ConsoleColor.Red;
            Console.BackgroundColor = ConsoleColor.Gray;
            Console.WriteLine("Colourful learning");
            Console.ReadKey();
        }
    }
}
  
  

Output


Colourful learning

Comments

Popular posts from this blog

System.Environment

System.Console

Datatype and keyword