03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 9 ❘ 651<br />

In this version, the Name and Courses values are auto-implemented properties. Because their<br />

set accessors are public methods, they invoke the class’s invariant method after they set<br />

their values.<br />

Because an auto-implemented property cannot be initialized in its declaration, this version’s<br />

constructor initializes the object’s Courses property so that property is not null when the constructor<br />

finishes.<br />

(You can find this code in the StudentTest example program in this chapter’s downloads.)<br />

4. The following code shows the revised class.<br />

public class Student<br />

{<br />

private string _Name;<br />

public string Name<br />

{<br />

get { return _Name; }<br />

set<br />

{<br />

if (value == null) throw new ArgumentNullException("Name",<br />

"Name must not be null");<br />

if (value.Length

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

Saved successfully!

Ooh no, something went wrong!