15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3objects <strong>and</strong> Types<br />

WhaT ’ s in This ChaPTer<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

The differences between classes <strong>and</strong> structs<br />

Class members<br />

Passing values by value <strong>and</strong> by reference<br />

Method overloading<br />

Constructors <strong>and</strong> static constructors<br />

Read - only fi elds<br />

Partial classes<br />

Static classes<br />

The Object class, from which all other types are derived<br />

So far, you ’ ve been introduced to some of the building blocks of the <strong>C#</strong> language, including variables, data<br />

types, <strong>and</strong> program flow statements, <strong>and</strong> you have seen a few very short complete programs containing<br />

little more than the Main() method. What you haven ’ t really seen yet is how to put all these together to<br />

form a longer, complete program. The key to this lies in working with classes — the subject of this chapter.<br />

Note that we cover inheritance <strong>and</strong> features related to inheritance in Chapter 4, “ Inheritance. ”<br />

This chapter introduces the basic syntax associated with classes. However, we assume<br />

that you are already familiar with the underlying principles of using classes — for<br />

example, that you know what a constructor or a property is. This chapter is largely<br />

confi ned to applying those principles in <strong>C#</strong> code.<br />

Classes <strong>and</strong> sTruCTs<br />

Classes <strong>and</strong> structs are essentially templates from which you can create objects. Each object<br />

contains data <strong>and</strong> has methods to manipulate <strong>and</strong> access that data. The class defi nes what data <strong>and</strong><br />

functionality each particular object (called an instance ) of that class can contain. For example, if<br />

you have a class that represents a customer, it might defi ne fi elds such as CustomerID , FirstName ,<br />

LastName , <strong>and</strong> Address , which you will use to hold information about a particular customer. It might<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!