How to Decide the Accessibility of Class Members in C Sharp

Design and implement your members as if you have already decided their accessibility level (usually this is done by implementing all members as public); , Start deciding the accessibility of each member by itself repeating the following steps on all...

2 Steps 1 min read Easy

Step-by-Step Guide

  1. Step 1: Design and implement your members as if you have already decided their accessibility level (usually this is done by implementing all members as public);

    For example, some other class might have code that accesses this member or uses it and because of the current level of accessibility chosen, it won't be able to access it anymore.

    Is your member designed to be used by other developers, and if so, will they be able to access it in its current accessibility level? In your future code, will changing the accessibility of this member be near impossible? Is your class intended to be inherited, and if so, will the inheriting class face troubles accessing this member (if it is allowed to)? Check if the answers to all the previous questions were "no" or not.

    If the answers were all "no"

    leave the accessibility level as it is and start deciding the accessibility of another member If one or more of the answers was "yes"

    increase the level of accessibility one degree and repeat the steps for the same member, from step 2
  2. Step 2: Start deciding the accessibility of each member by itself repeating the following steps on all members: Start by giving the member the lowest accessibility possible: (private) Answer the following questions: Will any existing code in your application or solution be affected by declaring the member on this accessibility level?

Detailed Guide

For example, some other class might have code that accesses this member or uses it and because of the current level of accessibility chosen, it won't be able to access it anymore.

Is your member designed to be used by other developers, and if so, will they be able to access it in its current accessibility level? In your future code, will changing the accessibility of this member be near impossible? Is your class intended to be inherited, and if so, will the inheriting class face troubles accessing this member (if it is allowed to)? Check if the answers to all the previous questions were "no" or not.

If the answers were all "no"

leave the accessibility level as it is and start deciding the accessibility of another member If one or more of the answers was "yes"

increase the level of accessibility one degree and repeat the steps for the same member, from step 2

About the Author

F

Frank Wells

Professional writer focused on creating easy-to-follow cooking tutorials.

86 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: