tance is required (unlike languages such as C++ and Eiffel, C# does not
support multiple inheritance of classes).
Properties, methods, and events
In the pure object-oriented paradigm, all functions are methods (this is the
case in Smalltalk). In C#, methods are only one kind of function member,
which also includes properties and events (there are others, too). Properties
are function members that encapsulate a piece of an object’s state, such as a
button’s color or a label’s text. Events are function members that simplify
acting on object state changes.
While C# is primarily an object-oriented language, it also borrows from the func‐
tional programming
paradigm. Specifically:
Functions can be treated as values
Through the use of delegates, C# allows functions to be passed as values to
and from other functions.
C# supports patterns for purity
Core to functional programming is avoiding the use of variables whose val‐
ues change, in favor of declarative patterns. C# has key features to help with
those patterns, including the ability to write unnamed functions on the fly
that “capture” variables (lambda expressions) and the ability to perform list
or reactive programming via query expressions. C# 6.0 also includes read-
only auto-properties to help with writing immutable (read-only) types.
Type Safety
C# is primarily a type-safe language, meaning that instances of types can interact
only through protocols they define, thereby ensuring each type’s internal consis‐
tency. For instance, C# prevents you from interacting with a string type as though it
were an integer type.
More specifically, C# supports static typing, meaning that the language enforces type
safety at compile time. This is in addition to type safety being enforced at runtime.
Static typing eliminates a large class of errors before a program is even run. It shifts
the burden away from runtime unit tests onto the compiler to verify that all the
types in a program fit together correctly. This makes large programs much easier to
manage, more predictable, and more robust. Furthermore, static typing allows tools
such as IntelliSense in Visual Studio to help you write a program, since it knows for
a given variable what type it is, and hence what methods you can call on that vari‐
able.
C# also allows parts of your code to be dynamically typed via
the dynamic keyword (introduced in C# 4.0). However, C#
remains a predominantly statically typed language.
2 | Chapter 1: Introducing C# and the .NET Framework
www.it-ebooks.info