
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
100
|
Chapter 3: Classes and Structures
The following code:
Console.WriteLine("Line.Parse(\"(12,2)(0,45)\") = " + Line.Parse("(12,2)(0,45)"));
Console.WriteLine("Line.Parse(\"(0,0)(0,0)\") = " + Line.Parse("(0,0)(0,0)"));
produces this output:
Line.Parse("(12,2)(0,45)") = (12,2) (0,45)
Line.Parse("(0,0)(0,0)") = (0,0) (0,0)
When implementing a Parse method on your own types, you need to
consider the situation in which invalid data is passed to this method.
When this happens, an
ArgumentException should be thrown. When a
null is passed in, you should instead throw an ArgumentNullException.
See Also
See the “Parse Method” topic and the parse sample under the “.NET Samples—How
To: Base Data Types” topic in the MSDN documentation.
3.4 Implementing Polymorphism
with Abstract Base Classes
Problem
You need to build several classes that share many common traits. These classes may
share common properties, methods, events, delegates, and even indexers; however,
the implementation of these may be different for each class. These classes should not
only share common code but also be polymorphic in nature. That is to say, code that
uses an object of the base class should be able to use an object of any of these derived
classes in the same manner.
Solution
Use an abstract base class to create polymorphic code. ...