Chapter 1. Introduction

C# is a language built specifically to program the new Microsoft .NET Framework. The .NET Framework consists of a runtime environment called the Common Language Runtime (CLR), and a set of base class libraries, which provide a rich development platform that can be exploited by a variety of languages and tools.

C# Language

Programming languages have strengths in different areas. Some languages are powerful but can be bug-prone and difficult to work with, while others are simpler but can be limiting in terms of functionality or performance. C# is a new language designed to provide an optimum blend of simplicity, expressiveness, and performance.

Many features of C# were designed in response to the strengths and weaknesses of other languages, particularly Java and C++. The C# language specification was written by Anders Hejlsberg and Scott Wiltamuth. Anders Hejlsberg is famous in the programming world for creating the Turbo Pascal compiler and leading the team that designed Delphi.

Key features of the C# language include the following:

Component orientation

An excellent way to manage complexity in a program is to subdivide it into several interacting components, some of which can be used in multiple scenarios. C# has been designed to make component building easy and provides component-oriented language constructs such as properties, events, and declarative constructs called attributes.

One-stop coding

Everything pertaining to a declaration in C# is localized to the declaration itself, rather than being spread across several source files or several places within a source file. Types do not require additional declarations in separate header or Interface Definition Language (IDL) files, a property’s get/set methods are logically grouped, documentation is embedded directly in a declaration, etc. Furthermore, because declaration order is irrelevant types don’t require a separate stub declaration to be used by another type.

Versioning

C# provides features such as explicit interface implementations, hiding inherited members, and read-only modifiers, which help new versions of a component work with older components that depend on it.

Type safety and a unified type system

C# is type-safe, which ensures that a variable can be accessed only through the type associated with that variable. This encapsulation encourages good programming design and eliminates potential bugs or security breaches by making it impossible for one variable to inadvertently or maliciously overwrite another.

All C# types (including primitive types) derive from a single base type, providing a unified type system. This means all types—structs, interfaces, delegates, enums, and arrays—share the same basic functionality, such as the ability to be converted to a string, serialized, or stored in a collection.

Automatic and manual memory management

C# relies on a runtime that performs automatic memory management. This frees programmers from disposing objects, which eliminates problems such as dangling pointers, memory leaks, and coping with circular references.

However, C# does not eliminate pointers: it merely makes them unnecessary for most programming tasks. For performance-critical hotspots and interoperability, pointers may be used, but they are only permitted in unsafe blocks that require a high security permission to execute.

Leveraging of the CLR

A big advantage of C# over other languages, particularly traditionally compiled languages such as C++, is its close fit with the .NET CLR. Many aspects of C# alias the CLR, especially its type system, memory-management model, and exception-handling mechanism.

Get C# Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.