C# and ASP.NET add-in
ReSharper. Productivity add-in to VS.NET for C# a
n
www.jetbrains.com/resharper
Ads by Google
The C# Station Tutorial
by Joe Mayo, 04/27/07, updated 2/22/08
Lesson 19: Encapsulation
Earlier in this tutorial, you learned about two of the
important principles of object-oriented programming,
Inheritance
and Polymorphism. Now that you've
seen much of the syntax of C#, I'll show you how
C# supports the another of the object-oriented
principles - Encapsulation. This lesson will discuss
Encapsulation with the following objectives:
z
Understand the object-oriented principle of
Encapsulation.
z
Learn the available modifiers for type members.
z
Protect object state throughproperties.
z
Control access tomethods.
z
Learn how to modify types for assembly
encapsulation
What is Encapsulation and How Does It
Benefit Me?
In object-oriented programming, you create objects
that have state and behavior. An object's state is the
data or information it contains. For example, if you
have a BankAccount object, its state could be
Amount and CustomerName. Behavior in an object is
often represented by methods. For example, the
BankAccount object's behavior could be Credit,
Debit, and GetAmount. This sounds like a nice
definition of an object, and it is, but you must also
consider how this object will be used.
When designing an object, you must think about how
others could use it. In a best-case scenario any
program using the object would be well designed and
the code would never change. However, the reality is
that programs do change often and in a team
environment many people touch the same code at
one time or another. Therefore, it is beneficial to
consider what could go wrong as well as the pristine
image of how the object *should* be used.
In the case of the BankAccount object, examine the
situation where code outside of your object could
Home About Articles/Docs Links Tutorials
页码,1/11C# Station: C# Tutorial Lesson 19 - Encapsulation
2008-8-18http://www.csharp-station.com/Tutorials/lesson19.aspx