Chapter 8. Inside Methods

In Chapter 7, you saw that classes consist of fields and methods . Fields hold the state of the object, and methods define the object’s behavior.

In this chapter, you’ll explore how methods work in more detail. You’ve already seen how to create methods, and in this chapter, you’ll learn about method overloading , a technique that allows you to create more than one method with the same name. This enables your clients to invoke the method with different parameter types.

This chapter also introduces properties. To clients of your class, properties look like member variables, but properties are implemented as methods. This allows you to maintain good data-hiding, while providing your clients with convenient access to the state of your class.

Chapter 7 described the difference between value types (such as int and long) and reference types. The most common value types are the “built-in” or “primitive” types (as well as structs), while the most common reference types are the user-defined types. This chapter explores the implications of passing value types to methods and shows how you can pass value types by reference, allowing the called method to act on the original object in the calling method.

Overloading Methods

Often you’ll want to have more than one method with the same name. The most common example of this is to have more than one constructor with the same name, which allows you to create the object with different types of parameters, or ...

Get Learning C# 2005, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.