}

 set {

  _ID = value;

 }

}

The value keyword contains the value that is being assigned by the set accessor. You normally assign the value of a property to a private member so that it is not visible to code outside the class, which in this case is _ID.

When you retrieve the value of a property, the get accessor is invoked:

public int ID {

 get {

  return _ID;

 }

 set {

  _ID = value;

 }

}

The following statement shows an example of retrieving the value of a property:

Console.WriteLine(c.ID); //---prints out 1234--- 

The really useful part of properties is the capability for you to perform checking on the value assigned. For example, before the ID property is set, you want to make sure that the value is between 1 and 9999, so you perform the check at the set accessor, like this:

public int ID {

 get {

  return _ID;

 }

 set {

  if (value > 0 && value <= 9999) {

   _ID = value;

  } else {

   _ID = 0;

  };

 }

}

Using properties, you can now prevent users from setting invalid values.

Read-Only and Write-Only Properties

When a property definition contains the get and set accessors, that property can be read as well as written. To make a property read-only, you simply leave out the set accessor, like this:

public int ID {

 get {

  return _ID;

 }

}

You can now read but not write values into the ID property:

Console.WriteLine(c1.ID); //---OK---

c1.ID = 1234; //---Error--- 

Likewise, to make a property write-only, simply leave out the get accessor:

public int ID {

 set {

  _ID = value;

 }

}

You can now write but not read from the ID property:

Console.WriteLine(c1.ID); //---Error---

c1.ID = 1234; //---OK---

You can also restrict the visibility of the get and set accessors. For example, the set accessor of a public property could be set to private to allow only members of the class to call the set accessor, but any class could call the get accessor. The following example demonstrates this:

public int ID {

 get {

  return _ID;

 }

 private set {

  _ID = value;

 }

}

In this code, the set accessor of the ID property is prefixed with the private keyword to restrict its visibility. That means that you now cannot assign a value to the ID property but you can access it: 

c.ID = 1234; //---error---

Console.WriteLine(c.ID); //---OK---

You can, however, access the ID property anywhere within the Contact class itself, such as in the Email property:

public string Email {

 get {

  //...

  this.ID = 1234;

  //...

 }

 //...

}

Partial Methods (C# 3.0)

Earlier on, you saw that a class definition can be split into one or more class definitions. In C# 3.0, this concept is extended to methods — you can now have partial methods. To see how partial methods works, consider the Contact partial class:

public partial class Contact {

 //...

 private string _Email;

 public string Email {

  get {

   return _Email;

  }

  set {

   _Email = value;

  }

 }

}

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату