Beruflich Dokumente
Kultur Dokumente
Here is the final iteration of the Motorcycle class (with one additional
constructor for the sake ofillustration). When chaining constructors, note how
the this keyword is dangling off the constructors eclaration (via a colon
operator) outside the scope of the constructor itself:
class Motorcycle
{
public int driverIntensity;
public string driverName;
// Constructor chaining.
public Motorcycle() {}
public Motorcycle(int intensity)
: this(intensity, "") {}
public Motorcycle(string name)
: this(0, name) {}
// This is the 'master' constructor that does all the real work.
public Motorcycle(int intensity, string name)
using the this keyword to chain constructor calls is never mandatory.
However, when you make use of this technique, you do tend to end up with a
more maintainable and concise class definition.
this keyword to chain constructor calls is never mandatory. However, when
you make use of this technique, you do tend to end up with a more
maintainable and concise class definition. Again, using this technique you can
simplify your programming tasks, as the real work is delegated to a single
constructor (typically the constructor that has the most parameters),
By definition, a utility class is a class that does not maintain any object-level
state and is not created with the new keyword. Rather, a utility class exposes
all functionality as class-level (a.k.a., static) members.
Under the .NET platform, System.Object is always the topmost parent in any
class hierarchy, which defines some general functionality for all types
There is another form of code reuse in the world of OOP: the
containment/delegation model also known as the has-a relationship or
aggregation. This form of reuse is not used to establish parent/child
relationships. Rather, the has-a relationship allows one class to define a
member variable of another class and expose its functionality (if required) to
the object user indirectly
The final pillar of OOP is polymorphism. This trait captures a languages
ability to treat related objects in a similar manner. Specifically, this tenant of
an object-oriented language allows a base class to define a set of members
(formally termed the polymorphic interface) that are available to all
descendents. A classs polymorphic interface is constructed using any
number of virtual or abstract members (see Chapter 6 for full details).
Within a set scope of a property, you use a token named value, which is
used to represent the incoming value used to assign the property by the
caller. This token is not a true C# keyword, ut is whatis known as a contextual
keyword.
// Read-only property? Error!
public int MyReadOnlyProp { get; }
Exceptions that are thrown by the .NET platform are (appropriately) called system exceptions.
These exceptions are generally regarded as nonrecoverable, fatal errors. System exceptions
derive directly from a base class named System.SystemException, which in turn derives from
System.Exception (which derives from System.Object):
such as ArgumentOutOfRangeException, IndexOutOfRangeException, StackOverflowException,
and so forth. Other namespaces define exceptions that reflect the behavior of that namespace.
For example, System.Drawing.Printing defines printing exceptions, System.IO defines
input/output-based exceptions, System.Data defines database-centric exceptions, and so forth.
}
}
A try/catch scope may also define an optional finally block. The purpose of a finally block is to ensure
that a set of code statements will always execute, exception (of any type) or not. To illustrate, assume
you want to always power down the cars radio before exiting Main(), regardless of any handled
exception:
If you did not include a finally block, the radio would not be turned off if an exception is
encountered (which might or might not be problematic). In a more real-world scenario, when you need
to dispose of objects, close a file, or detach from a database (or whatever), a finally block ensures a
location for proper cleanup.
If you fail to handle an exception thrown by a method in the .NET base class libraries, the
Visual Studio
debugger breaks at the statement that called the offending method.
catch keyword and an optional finally scope.
When you are creating your own custom exceptions, you ultimately create a class type deriving from
System.ApplicationException, which denotes an exception thrown from the currently executing
application. In contrast, error objects deriving from System.SystemException represent critical (and
fatal)
errors thrown by the CLR.