Visual C# 1002005A Developers Notebook [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Visual C# 1002005A Developers Notebook [Electronic resources] - نسخه متنی

Jesse Liberty

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



1.11. Limit Access Within Properties

It is now possible to restrict the accessibility level of the get and set accessors within a property using access modifiers. Usually you would restrict access to the set accessor and make the get accessor public.



Note: Now you can restrict the accessibility level of the get and set accessors within a property.


1.11.1. How do I do that?

Add the access modifier to either the get or the set accessor within the property, as illustrated in Example 1-9.Example 1-9. Limiting access to property accessors#region Using directives using System; using System.Collections.Generic; using System.Text; #endregion namespace LimitPropertyAccess { public class Employee { private string name; public Employee(string name) { this.name = name; } public string Name { get { return name; } protected set { name = value; } } public virtual void ChangeName(string name) { // do work here to update many records Name = name; // access the private accessor } } class Program { static void Main(string[ ] args) { Employee joe = new Employee("Joe"); // other work here string whatName = joe.Name; // works // joe.Name = "Bob"; // doesn't compile joe.ChangeName("Bob"); // works Console.WriteLine("joe's name: {0}", joe.Name); } } }

Output:joe's name: Bob

1.11.2. What just happened?

The design of your Employee class calls for the string name to be private. You anticipate that one day you'll want to move this to a database field, so you resolve that all access to this field will be through a property, Name.

Other classes are free to access Name, but you do not want them to set the name directly. If they are going to change the name field, they must do so through the ChangeName virtual method. You anticipate that derived classes will do different work when an employee changes his name.

Thus you want to provide access to the set accessor to this class's methods and to methods of any class that derives from this class, but not to other classes. You accomplish this by adding the restricting access modifier protected to the set accessor:protected set { name = value; }

1.11.3. What about . . .

...restrictions on using

access modifiers?

You cannot use these modifiers on interfaces or explicit interface member implementations. You can use them only if both get and set are included, and you can use them only on one or the other.

Further, the modifier must restrict access, not broaden it. Thus, you cannot make the property protected and then use a modifier to make get public.

1.11.4. Where can I learn more?

You can learn more about properties and access modifiers by reading the MSDN article on properties at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfPropertiesPG.asp.

/ 91