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.

Classes ❘ 73<br />

}<br />

{<br />

}<br />

// do whatever needs to be done to set the property.<br />

The get accessor takes no parameters <strong>and</strong> must return the same type as the declared property. You should<br />

not specify any explicit parameters for the set accessor either, but the compiler assumes it takes one<br />

parameter, which is of the same type again, <strong>and</strong> which is referred to as value. As an example, the following<br />

code contains a property called Age, which sets a field called age. In this example, age is referred to as the<br />

backing variable for the property Age.<br />

private int age;<br />

public int Age<br />

{<br />

get<br />

{<br />

return age;<br />

}<br />

set<br />

{<br />

age = value;<br />

}<br />

}<br />

Note the naming convention used here. You take advantage of <strong>C#</strong>’s case sensitivity by using the same name,<br />

Pascal-cased for the public property, <strong>and</strong> camel-cased for the equivalent private field if there is one. Some<br />

developers prefer to use field names that are prefixed by an underscore: _age; this provides an extremely<br />

convenient way of identifying fields.<br />

Read-Only <strong>and</strong> Write-Only Properties<br />

It is possible to create a read-only property by simply omitting the set accessor from the property<br />

definition. Thus, to make Name a read-only property, you would do the following:<br />

private string name;<br />

public string Name<br />

{<br />

get<br />

{<br />

return Name;<br />

}<br />

}<br />

It is similarly possible to create a write-only property by omitting the get accessor. However, this is<br />

regarded as poor programming practice because it could be confusing to authors of client code. In general, it<br />

is recommended that if you are tempted to do this, you should use a method instead.<br />

Access Modifiers for Properties<br />

<strong>C#</strong> does allow the set <strong>and</strong> get accessors to have differing access modifiers. This would allow a property<br />

to have a public get <strong>and</strong> a private or protected set. This can help control how or when a property can be<br />

set. In the following code example, notice that the set has a private access modifier <strong>and</strong> the get does not<br />

have any. In this case, the get takes on the access level of the property. One of the accessors must follow the<br />

access level of the property. A compile error will be generated if the get accessor has the protected access<br />

level associated with it because that would make both accessors have a different access level<br />

from the property.<br />

public string Name<br />

{<br />

get<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!