For Private Class, should i declare my property As Public Or Friend (internal in c#)? My Private Class is not accessible by any other classes except it parent class.
Access level of property in Private Class
775 views Asked by san louise At
2
There are 2 answers
0
On
Since you class (Nested one) is private
the only one who can access it is its container (the base or parent class). In this case public
or internal
(Friend
in VB) doesn't make any difference, since the only one who can access the nested class is it's parent.
In general, if some day you want to change the access modifier to public
then its good to foresee it now and choose between internal
and public
. Its simple, if you want members of your nested class to be seen only inside the assembly where it defined, then use internal
otherwise consider using public
.
In general, try to keep your encapsulation intact. The relationship between your classes should be on "Need to know" basis.
Since I am not familiar with your application design,I am sure I am oversimplifying, but I would ask myself the following question:
Is the parent class the only class that needs to know the subclass? if so, It should be
private
.If not, are all other entities that needs to about know the subclass are in the same project? if so, It should be
internal
.If there are entities external to the project that needs to know about the subclass, it should be
public
.As to the properties. If you decided to have the subclass
private
, there is no use in making the propertiespublic
.The only object that can have an instance of the class is the parent class, which is in the same project, and therefore
internal
should be enough for the properties.Note
C# does not have a
friend
modifier. Instead of that you can useinternal
,or The-most similar-yet-less-elegant option: InternalsVisibleTo