Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have defined my class below. if I change visibilty for Assign method from internal to private, what is good and bad side of this change?

method Assign() is only used inside this class.

C#
public class AxisCap
 {
    internal AxisCap (string initString)
     {
         this.Assign (initString);
     }

    internal void Assign (string initString)
     {
      //omitted contents here
 }
  }


thanks for the answers. the real intention of my change is to obfuscate this method when it is changed to private.
Posted
Updated 28-Mar-15 10:13am
v2

Just read on the topic: https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx[^].

The question is the change of access modifier it total absurd. Let's say, "private" is a good modifiers for members you want to keep private. :-) And so on.

—SA
 
Share this answer
 
As internal marked classes or class-members are accessible from inside the same namespace assembly they belong to. They're quasi public within that namespace assembly. It's a comparatively rarely used access modifier and care should be taken when to use it in order not to abuse its "almost-public" nature.

As private marked class-members are only accessible from within the class they belong to.

There's nothing inherently good or bad about choosing the one or the other - it just depends. Is your Assign-Method meant to be used only by the class AxisCap? Then make it private. Should only AxisCap and classes inheriting from it be able to use it? Then make it protected. Should it be accessible from classes within the same namespace assembly? Make it internal. Should it be accessible from anywhere, make it public.

It's just important that you have an idea for an overall design of your solution and how the different parts work together. Always use the most strict modifier possible.

Edit: strikeouts
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-Mar-15 22:35pm    
I'm really sorry, but you made so big mistake that I have no choice to vote 1 this time.
This mistake is so fundamental, that fixing it can change the big part of your understanding of .NET programming.

"Internal" allows access not to "same namespace". It's "same assembly". This mistakes is very fundamental, because it tells the tale: maybe you idea of namespaces is deeply wrong. One of the main ideas of namespaces is: they cannot affect any properties of the code whatsoever, they just makes the names of top-level types much longer, to alleviate possible name clashes. And, as you probably understand, changing any names cannot change anything essential.

I would not explain any of the particular modifiers. The inquirer can just read one of the MSDN pages on the topic. And you too. Please see my answer.

Again, really sorry.

—SA
[no name] 21-Mar-15 22:44pm    
No need to be sorry :) I'm actually surprised that I made that mistake - because I know about the effect of the InternalsVisibleToAttribute. Seems like there were two competing assumptions in my head ;-) Thank you for correcting me.
- Sebastian
Sergey Alexandrovich Kryukov 21-Mar-15 22:45pm    
Well... very good. I re-voted. :-)
—SA
[no name] 21-Mar-15 22:49pm    
Большое спасибо, Сергей :-)
You can change it to private. If you do, this method will not be accessible to other classes (within the same namespace) that use this class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900