Intermediate C# Skills
In this chapter
Reference Types
Object Construction/ Destruction
Object-Oriented Features
Exception Handling
In Brief
Reference Types
As discussed in Chapter 2, reference types differ from value types in that reference types are allocated on the heap and manipulated with variables that point to these objects.
The particular reference type we'll spend the most time with is the class, which permits creation of custom types. Other reference types include delegates and interfaces. Delegates are type-safe method references that allow code to connect to events dynamically. Interfaces specify a contract that a set of classes agree to expose to the public. Listing 3.1 shows a basic class declaration.
The class declaration in Listing 3.1 is simple and it compiles, but it's not useful. To make it useful, it should have a visibility modifier, such as public, if it was to be a library type, instantiated by other code. Otherwise, it would need a Main method, as shown in other listings throughout this book. Members could include fields, methods, properties, indexers, and events. A class can also nest other types, including other classes, delegates, and enums. The rest of this chapter and Chapter 4 explain classes, their members, and other types, which can be nested in classes or standalone.
Listing 3.1 A Basic Class Declaration (BasicClass.cs)
class MyClass { // members }