Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | This lecture is about operations in classes and structs. In particular, we are concerned with operations that access the encapsulated data in a type. C# offers several different kinds of operations. Methods are the most well-known, but C# also supports properties, indexers, and overloaded operators. |
Accessing Data in Objects |
Indirect data access Slide Annotated slide Contents Index References Textbook | Avoiding direct access to data is one of the main themes of object-oriented programming. We first introduce the idea. After that we will study properties, which - in C# - help us a lot to access data indirectly. |
|
|
Overview of data access in C# Slide Annotated slide Contents Index References Textbook | Our interest is indirect data access. On this page we take a very broad look at data access, including direct access to data. |
|
|
Properties |
Properties - Basic Use Slide Annotated slide Contents Index References |
Program: A Class C with a simple properties - getter and setter. |
|
Program: A client of class C. |
|
Properties - Tricky Use Slide Annotated slide Contents Index References |
Program: A Class C with a simple properties - getter and setter. |
|
Program: A client of class C. |
|
Properties in C# Slide Annotated slide Contents Index References Textbook | Properties introduce getter and setter methods in C# - in a very convenient way. You can think of a property as a method. It is important to realize, however, that you cannot tell the difference between an activation of a property and use of a variable. An activation of a method M reveal itself by the required parentheses after M: M(...). |
We introduce properties by a number of examples. On this page we provide a number of introductory examples of
a Balance property of class BankAccount. |
Program: A BankAccount class with a trivial Balance property together with Deposit and Withdraw methods. |
|
Program: A BankAccount class with a Balance property - without a balance instance variable. |
|
Program: A client program. |
|
Program: Output from the client program. |
|
Program: A BankAccount class with a disciplined Balance property. |
|
Program: A client of the disciplined BankAccount. |
|
Program: Output of the this client. |
|
Exercise 5.2. A funny BankAccount | In this exercises we provide a version of class BankAccount with a "funny version" of the Balance property. You should access the exercise via the web version, in order to get access to the source programs involved. Study the Balance property of the funny version of class BankAccount. Explain the behaviour of the given client of the funny BankAccount. Next test-run the program and confirm your understanding of the two classes. Please notice how difficult it is to follow the details when properties - like Balance in the given version of class BankAccount - do not pass data directly to and from the instance variables. |
Read more about this topic in the text book version of this material. |
Properties: Class Point with polar coordinates Slide Annotated slide Contents Index References Textbook | The example on this page is a typical, real-life illustration of the usefulness of properties. |
|
Program: Class Point with polar data representation. Both rectangular and polar access
is provided via properties. |
|
Exercise 5.3. Point setters | In the Point class on the accompanying slide we have shown how to program getter properties in the class Point. Extend the four properties with setters as well. The new version of this class will support mutable points. Write a small client program that demonstrates the use of the setter properties. Hint: Please be aware that the revised class should allow us to get and set the rectangular and polar coordinates (x, y, angle, radius) of a point independent of each other. You should first consider what it means to do so. |
|
Read more about this topic in the text book version of this material. |
Automatic Properties Slide Annotated slide Contents Index References Textbook |
|
Program: Class BankAccount with two automatic properties. |
|
Program: An equivalent BankAccount class without automatic properties. |
|
Program: A Client of class BankAccount. |
|
Program: Output from the Client program. |
|
|
Object Initialization via Properties Slide Annotated slide Contents Index References Textbook |
|
Program: Class BankAccount - as short as possible. |
|
Program: A client of the simple BankAccount with object initializers. |
|
Program: An equivalent BankAccount class without object initializers. |
|
Program: Class BankAccount - with two constructors. |
|
Program: A client of class BankAccount with an object initializer. |
|
Program: An equivalent client of class BankAccount without object initializers. |
|
Program: Output of the Client program. |
|
Program: Class BankAccount and class Person. |
|
Program: A client of class BankAccount with nested object initializers. |
|
|
Summary of properties in C# Slide Annotated slide Contents Index References Textbook | We mention and summarize some important properties of properties. |
Syntax: The syntax of a C# property. Here we show both the getter and setter. At least one of them must be present. |
|
|
Naming conventions - pertain to coding style. |
Indexers |
Indexers in C# Slide Annotated slide Contents Index References Textbook | Indexers are a kind of properties. Indexers let you use array notation on objects that are instances of your own classes. |
|
Program: A Class A with an indexer. This is a simple and somewhat artificial example of the use of indexers.
It is illustrated how three individual variables d, e, and f in class A can be accessed
as a[1], a[2], a[3], where a is an instance of class A. |
|
Program: A client of A which uses the indexer of A. |
|
Program: Output from Main in class B. |
|
Program: The class BitArray. This is example shows a fine and realistic use of an indexer. The indexer
accesses the individual bits in a bit array. The bit array is represented as
a (relatively small) array of 32 bit integers. This example is taken from
the C# language specification. Please consult the text book version for
an explanation of the details. |
|
Program: A client of class BitArray. |
|
|
Associative Arrays Slide Annotated slide Contents Index References Textbook | Associative arrays are generalized arrays that allow indexing via objects of an arbitrary type (not just integers). Indexers provide a natural notation for associative arrays. |
The concept associative array: An associative array is an array which allows indexing by means of arbitrary objects, not just integers |
|
Program: The class A indexed by a string. An example similar to one of the previous indexer examples.
In this example it is illustrated how to index via objects of type string. |
|
Program: A client of A which uses the string indexing of A. |
|
Program: Output from Main in class B. |
|
|
In the lecture about collections, specifically in the interface IDictionary<K,V> we will see how indexers are can be used to access objects of type V via objects of type K by use of the notation dict[k], where dict is a dictionary and k is a key of type K. |
Example of associating Person with BankAccount Slide Annotated slide Contents Index References |
|
Program: The class Person. |
|
Program: The class BankAccount - without owner. |
|
Program: The class PersonAccountPair. |
|
Program: The class Accounts - the associative array class. |
|
Program: A client of Accounts. |
|
|
Summary of indexers in C# Slide Annotated slide Contents Index References Textbook | On this slides we mention a number of characteristics of indexers in C#. |
Syntax: The syntax of a C# indexer |
|
|
Methods |
Methods in C# Slide Annotated slide Contents Index References Textbook | Properties and indexers are new to most readers. We should be careful, however, not to forget about methods. In most object-oriented programs, most operations will be implemented as methods. In this section of the material we will discuss - and review - the method concept, local variables in methods, and parameter passing modes in C#. |
|
|
Syntax: The syntax of a method in C# |
|
|
Local variables in methods Slide Annotated slide Contents Index References Textbook | We start with a brief overview of local variables, and in particular their initialization compared with the initialization of instance variables contained in classes. |
|
|
Program: Local variables and instance variables - initialization and default values. In this example we illustrate differences between
local variables and instance variables with respect to initialization. |
|
Read more about local variables in the text book version of this material. |
Parameters Slide Annotated slide Contents Index References Textbook | On this page we will review the basic concepts of method parameters: formal parameters and actual parameters. We will also enumerate the different kinds of parameter passing modes. |
|
|
The parameter passing modes. |
Read more about parameters in the text book version of this material. |
Value Parameters Slide Annotated slide Contents Index References Textbook | Value parameters are - by far - the most frequently used parameters of methods in C#. |
|
How value parameters work. |
Program: Methods with value parameters in class BankAccount. |
|
|
Read more about value parameters in the text book version of this material. |
Passing references as value parameters Slide Annotated slide Contents Index References Textbook | Be warned! This is not a discussion of C# reference parameters. It is a discussion of how to pass references via value parameters. |
|
Program: The class Date and the method DayDifference with a value parameter. This class introduces the DayDifference method, which accepts a formal Date parameter. |
|
Program: The class Person which calls DayDifference on dateOfBirth. We pass a reference to a Date object as an actual parameter. |
|
Program: A client of Person and Date which reveal the consequences. A class with a Main method which - indirectly - calls the method of interest. |
|
Program: Client program output. The output that appears if you run the program shown above. |
|
Exercise 5.4. Passing references as ref parameters | In the Date and Person classes of the corresponding slide we pass a reference as a value parameter to method DayDifference in class Date. Be sure to understand this. Read about ref parameters later in this lecture. Assume in this exercise that the formal parameter other in Date.DayDifference is passed by reference (as a C# ref parameter). Similarly, the actual parameter dateOfBirth to DayDifference should (of course) be passed by reference (using the keyword ref in front of the actual parameter). What will be the difference caused by this program modification. Test-drive the program with a suitable client to verify your answer. |
This is the main point to remember from this page! |
Read more about references passed by value in the text book version of this material. |
Passing structs as value parameters Slide Annotated slide Contents Index References Textbook | On this page we repeat the example from the previous page - but now on a struct instead of a class. |
|
Program: The struct Date and the method DayDifference with a value parameter. The exact similar setup as on the previous page. |
|
Program: The class Person which calls DayDifference on dateOfBirth. |
|
Program: A client of Person and Date which reveal the consequences. |
|
Program: Client program output. |
|
Exercise 5.5. Passing struct values as ref parameters | This exercise corresponds to the similar exercise on the previous slide. In the Date struct and the Person class of this slide we pass a struct value as a value parameter to the method DayDifference. Assume in this exercise that the formal parameter other in Date.DayDifference is passed by reference (a C# ref parameter). Similarly, the actual parameter dateOfBirth to DayDifference should (of course) be passed by reference (using the keyword ref in front of the actual parameter). What will be the difference caused by this program modification. You should compare with the version on on the slide. Test-drive the program with a suitable client to verify your answer. |
|
Read more about struct values passed by value in the text book version of this material. |
Reference Parameters Slide Annotated slide Contents Index References Textbook | Here we discuss ref parameters ala C#. This is analogous to var (variable parameters) known from Pascal. |
|
A formal ref parameter serve as an alias - an alternative name - of the corresponding actual parameter. |
Program: The class A with a Swap method. A classical use of a reference parameter. The Swap method
exchanges the values of two variables. |
|
Program: Output of class A with Swap. |
|
Read more about reference parameters in the text book version of this material. |
Output Parameters Slide Annotated slide Contents Index References Textbook | out parameters in C# are reference parameters used only for output purposes. |
|
A formal ref parameter serve as an alias - an alternative name - of the corresponding actual parameter. The formal parameter must be assigned in the method, hereby facilitating output from the method. |
Program: The class A with a DoAdd method. The DoAdd method adds three value parameters and passes the result back to the caller
via an out parameter. |
|
Program: Output of class A with DoAdd. |
|
Read more about output parameters in the text book version of this material. |
Use of ref and out parameters in OOP Slide Annotated slide Contents Index References Textbook | On this we discuss the usefulness of ref and out parameters in object-oriented programming. |
|
How to handle the situation where two or more pieces of output is required from a method. Notice that out parameters provides one out of several different solutions. |
|
Read more about use of ref and out parameters in OOP in the text book version of this material. |
Parameter Arrays Slide Annotated slide Contents Index References Textbook | Parameter arrays in C# makes it possible to pass many different actual parameters - of the same type - to a single array parameter. |
|
|
Program: The class A with a DoAdd method - both out and params. A variant of the DoAdd method where all but the first parameters are summed together and
returned in the first parameter, which is an out parameter. |
|
Program: Output of class A with DoAdd - both out and params. |
|
Program: An integer sequence - use of params in constructor. It is possible to have a constructor with a parameter array. |
|
|
Read more about parameter arrays in the text book version of this material. |
Extension Methods Slide Annotated slide Contents Index References Textbook | 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. |
|
|
Program: Motivation: A Client of Point without use of DistanceTo. |
|
Program: A Client of class Point which uses an extension method DistanceTo. The method DistanceTo is used as an instance method in this program... |
|
Program: The static class PointExtensions. ... but actually implemented as a (somewhat special) static method in
the static class PointExtensions.
|
|
|
Exercise 5.6. Extending struct Double | At the accompanying page we have seen how class Point can be extended with the instance method DistanceTo. This is an extension method. If you study the method DistanceTo you will see that we use the squareroot function Math.Sqrt, defined statically in class Math. And we could/should have used a similar Square function had it been available. It is remarkable that C# 3.0 allows us to extend the structs behind the primitive types, such as Double and Int32. Write a static class that extends struct Double with the two extension methods Sqrt and Square. Afterwards, rewrite the method DistanceTo such that it makes use of the new instance methods in struct Double. |
Methods versus Properties versus Indexers Slide Annotated slide Contents Index References Textbook | We summarize when to use methods, properties, and indexers. |
|
|
Chapter 5: Data Access, Properties, and Methods
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:14:55