Page 28 : 29
Object-oriented Programming in C#
Data Access, Properties, and Methods
* Accessing Data in Objects
Indirect data access
Overview of data access in C#
Properties - Basic Use
Properties - Tricky Use
Properties in C#
Properties: Class Point with polar coordinates
Object Initialization via Properties
Summary of properties in C#
Indexers in C#
Example of associating Person with BankAccount
Summary of indexers in C#
Methods in C#
Local variables in methods
Passing references as value parameters
Passing structs as value parameters
Use of ref and out parameters in OOP
Methods versus Properties versus Indexers
Have you ever wanted to add a new method to an existing class, without defining a subclass? If so, extension methods - as introduced in C# 3.0 - is the solution to your problem.
In C# 3.0 an extension method defines an instance method in an existing class without altering the definition of the class.
Class Point and a client
Motivation: A Client of Point without use of DistanceTo.
A Client of class Point which uses an extension method DistanceTo.
The method DistanceTo is used as an instance method in this program...
The static class PointExtensions.
... but actually implemented as a (somewhat special) static method in the static class PointExtensions.
An extension method
extends the type of the first parameter
is defined as a static method with a
modifier on the first parameter
must be defined in a static class
cannot access private instance variables in the extended class
is called by - behind the scene - translating to a call of the static method
Extending struct Double