O'Reilly C# 5.0 in a Nutshell, 5th Edition 9781449320102 Manual Do Utilizador

Códigos do produto
9781449320102
Página de 21
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.
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 man-
age, 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 variable.
C# also allows parts of your code to be dynamically typed via
the 
dynamic
 keyword (introduced in C# 4). However, C# re-
mains a predominantly statically typed language.
C# is also called a strongly typed language because its type rules (whether enforced
statically or at runtime) are very strict. For instance, you cannot call a function that’s
designed to accept an integer with a floating-point number, unless you first explic-
itly
 convert the floating-point number to an integer. This helps prevent mistakes.
Strong typing also plays a role in enabling C# code to run in a sandbox—an envi-
ronment where every aspect of security is controlled by the host. In a sandbox, it is
important that you cannot arbitrarily corrupt the state of an object by bypassing its
type rules.
Memory Management
C# relies on the runtime to perform automatic memory management. The Common
Language Runtime has a garbage collector that executes as part of your program,
reclaiming memory for objects that are no longer referenced. This frees programmers
2 | Chapter 1:
Introducing C# and the .NET Framework