15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Classes ❘ 67<br />

class PhoneCustomer<br />

{<br />

public const string DayOfSendingBill = "Monday";<br />

public int CustomerID;<br />

public string FirstName;<br />

public string LastName;<br />

}<br />

Events are class members that allow an object to notify a caller whenever something noteworthy happens,<br />

such as a field or property of the class changing, or some form of user interaction occurring. The client can<br />

have code, known as an event h<strong>and</strong>ler, which reacts to the event. Chapter 8, “Delegates, Lambdas, <strong>and</strong><br />

Events,” looks at events in detail.<br />

function members<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

Methods<br />

Function members are those members that provide some functionality for manipulating the data in<br />

the class. They include methods, properties, constructors, finalizers, operators, <strong>and</strong> indexers.<br />

Methods are functions that are associated with a particular class. Just as with data members, function<br />

members are instance members by default. They can be made static by using the static modifier.<br />

Properties are sets of functions that can be accessed from the client in a similar way to the public<br />

fields of the class. <strong>C#</strong> provides a specific syntax for implementing read <strong>and</strong> write properties on your<br />

classes, so you don’t have use method names that have the words Get or Set embedded in them.<br />

Because there’s a dedicated syntax for properties that is distinct from that for normal functions, the<br />

illusion of objects as actual things is strengthened for client code.<br />

Constructors are special functions that are called automatically when an object is instantiated.<br />

They must have the same name as the class to which they belong <strong>and</strong> cannot have a return type.<br />

Constructors are useful for initialization.<br />

Finalizers are similar to constructors but are called when the CLR detects that an object is no longer<br />

needed. They have the same name as the class, preceded by a tilde (~). It is impossible to predict precisely<br />

when a finalizer will be called. Finalizers are discussed in Chapter 13, “Memory Management<br />

<strong>and</strong> Pointers.”<br />

Operators, at their simplest, are actions such as + or –. When you add two integers, you are, strictly<br />

speaking, using the + operator for integers. However, <strong>C#</strong> also allows you to specify how existing<br />

operators will work with your own classes (operator overloading). Chapter 7, “Operators <strong>and</strong> Casts,”<br />

looks at operators in detail.<br />

Indexers allow your objects to be indexed in the same way as an array or collection.<br />

Note that official <strong>C#</strong> terminology makes a distinction between functions <strong>and</strong> methods. In <strong>C#</strong> terminology,<br />

the term “function member” includes not only methods, but also other nondata members of a class<br />

or struct. This includes indexers, operators, constructors, destructors, <strong>and</strong> also — perhaps somewhat<br />

surprisingly — properties. These are contrasted with data members: fields, constants, <strong>and</strong> events.<br />

Declaring Methods<br />

In <strong>C#</strong>, the definition of a method consists of any method modifiers (such as the method’s accessibility), the<br />

type of the return value, followed by the name of the method, followed by a list of input arguments enclosed<br />

in parentheses, followed by the body of the method enclosed in curly braces:<br />

[modifiers] return_type MethodName([parameters])<br />

{<br />

// Method body<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!