Summary


Type members are the programming constructs used to build a class or struct. This chapter briefly touched on each type member and usage guidelines associated with their usage.

Constants are used to provide readability and act as a single point of change for known values. Constants are shared members by default and are used whenever values are guaranteed not to change.

Fields are variables used to hold state in a class or struct. To provide data encapsulation, fields are commonly created with the private access modifier. Public fields lock the class interface and data together, making it difficult to change the code without breaking compatibility.

Properties represent a piece of data exposed by the class. Properties extend the concept of a field by adding a layer of abstraction between a class's internal and external representation. Properties give the designer the ability to change how state is managed within the class while not affecting how state is managed outside the class. Properties can be designated as read-only or write-only, simply by excluding the getter or setter, to prevent unwanted usage of the property.

Methods perform the functionality of the class. Multiple methods can have the same name with different signatures to provide multiple implementations. Such a practice is called method overloading.

Fields, properties, and methods can all be declared as static. Static type members are accessible without an object reference and normally address things that you would associate with the class's general concept not a specific instance.

Events provide a way for classes to notify listeners when certain actions have occurred. Since events are not required to be handled, they should typically be used to send information not critical to the central operation of the class.

Constructors allow you to customize the object creation process. If no constructor is specified the C# compiler will inject a default constructor into the class's MSIL.

Type members have levels of accessibility. A public member has no access restrictions. Protected members are accessible externally from derived classes only. Internal members are accessible from any code contained in the same namespace. Private members are accessible only from within the containing class.

System.Object is the root of every type in the .NET Framework. Because all types inherit from this base, all types have certain methods available to them..




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