Section 1.11. Limit Access Within Properties


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.



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net