Chapter 3. Objects and Types

WHAT'S IN THIS CHAPTER?

  • The differences between classes and structs

  • Class members

  • Passing values by value and by reference

  • Method overloading

  • Constructors and static constructors

  • Read-only fields

  • Partial classes

  • Static classes

  • The Object class, from which all other types are derived

So far, you've been introduced to some of the building blocks of the C# language, including variables, data types, and program flow statements, and you have seen a few very short complete programs containing little more than the Main() method. What you haven't really seen yet is how to put all these together to form a longer, complete program. The key to this lies in working with classes — the subject of this chapter. Note that we cover inheritance and features related to inheritance in Chapter 4, "Inheritance."

Note

This chapter introduces the basic syntax associated with classes. However, we assume that you are already familiar with the underlying principles of using classes—for example, that you know what a constructor or a property is. This chapter is largely confined to applying those principles in C# code.

CLASSES AND STRUCTS

Classes and structs are essentially templates from which you can create objects. Each object contains data and has methods to manipulate and access that data. The class defines what data and functionality each particular object (called an instance) of that class can contain. For example, if you have a class that represents a customer, it might define fields ...

Get Professional C# 4 and .NET 4 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.