When to use Public and Private access modifiers

 Posted by Poster on 10/29/2010 | Category: C# Interview questions | Views: 2537 | Points: 40

Using proper access modifiers is the key of writing good Object Oriented Programming.

Below are few of the points that should be taken care while using Public and Private access modifiers

1. A member that are used within a class should be private.
2. If you are refactoring a lengthy method in a class, the re-factored method should be declared as private as you are not intending to reuse that method again.
3. Any member that has some sensitive information or calculations should be controlled by using private and it should be exposed properly through a public member.

1. Frequently used methods should be public.
2. The methods of different layers that are intended to pass data between layers should be declared as public.
3. Generally properties in C# is declared as public as it is intended to use by anyone.
4. Method that gets and sets the value of private data should be public. (For example, Properties)

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response