Chapter 2: Type Members


Overview

One of the most basic building blocks in object-oriented programming is the class. Apart from interfaces and structs, the majority of application code that you will write for the .NET platform will be encapsulated in classes. Becoming an effective class designer is essential to becoming a successful object-oriented programmer.

Effective class designs also simplify maintenance and versioning. Once a class is designed and clients are consuming the class, it becomes difficult to change or version. If you remove functionality from a production class, you have violated the contract that consumers have coded against, and the consumer's code will fail. If you alter an existing class, you could possibly break all consumers using the class. Much like building a house, the original design and thought process is critical to avoid making future changes. It is much more difficult and expensive to redesign a finished house than one being sketched. Similarly, it becomes more difficult to change the design of your class structure once the class is in the production environment.

Members defined inside a type, or type members, are the fundamental programming constructs used to build classes within the .NET Framework. Type members within the .NET Framework include:

  • Constants

  • Variables (also known as fields)

  • Properties

  • Methods

  • Constructors

  • Events

All object-oriented programming languages (for example, C++, Java, C#, and VB.NET) offer differing levels of support for type members. For example, C++ and Java do not have support for a property; they make use of get and set methods. However, within the .NET Framework, all languages must support the CTS. The CTS enforces a set of common rules and requirements for describing type members.

This chapter introduces the .NET type members as described by C#. It is important to understand that other programming languages targeting the .NET platform (for example VB.NET), offer similar type members. Also, once compiled to the .NET native intermediate language, it becomes very difficult to tell what language the code was actually written in.

We also examine the class member accessibility levels that .NET provides to restrict the visibility of type members. At times, classes need the ability to restrict what type members are visible to different pieces of code. For example, a class may need to have a function that is only callable from within the same namespace. Applying the proper access modifier gives us the flexibility to provide such restrictions.

Lastly, we will also discuss inherent type members available to class designers. We will uncover functionality found in the .NET Framework's root class, System.Object. These inherent type members, or default methods are always present; this is due to the fact that every class is derived from System.Object. While the default methods are useful, it is also possible to override these type members to provide specific functionality. System.Object defines useful methods that client code can invoke on any kind of object; for example, the Equals() method indicates whether two objects contain the same value. We'll survey these methods during the chapter, and describe how to use the most important ones




C# Class Design Handbook(c) Coding Effective Classes
C# Class Design Handbook: Coding Effective Classes
ISBN: 1590592573
EAN: 2147483647
Year: N/A
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net