New Features in C# 2.0—Limit Access: What just happened?

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Tutorials
C# Tutorials

New Features in C# 2.0

© 2005 O'Reilly Media, Inc.

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; }


Previous_Page_.gif Next_Page_.gif


Personal tools