locked
Difference between Internal and Private access modifiers. RRS feed

  • Question

  • Hi,

         I have this class1.cs in my class library project 

    namespace ClassLibrary1
    {
      public class ClassPublic
      {
        public int publicInt;
        //internal int internalInt; 
      }
    
      internal class ClassInternal
      {
      }
    
      class ClassPrivate
      {
      }
    }

    Now, I have referenced it in my other project and when I try to instantiate ClassInternal and ClassPrivate it is giving error ( Can't access because of it protection level).

              Generally ClassInternal will not be having access out side of its assembly. If so

    What is the difference between Internal and Private. If possible can you any body explain me with an example.

     

    Thanks

    Srikanth Gunnala

     


    Regards, Srikanth Gunnala
    Monday, April 26, 2010 7:05 AM

Answers

  • Hi,

    You can access private members only inside of the class, but internal members are accessable inside of the assembly. i.e. all classes inside of an assembly can access an internal member (share inside of the assembly) but aren't accessible from other assemblies.

    Regards,

    Yasser.


    Don't be stickler and wine with William Shakespeare after the solution :^)
    "And this our life, exempt from public haunt, finds tongues in trees, books in the running brooks, sermons in stones, and good in everything." William Shakespeare
    Monday, April 26, 2010 7:15 AM

All replies

  • Hi,

    You can access private members only inside of the class, but internal members are accessable inside of the assembly. i.e. all classes inside of an assembly can access an internal member (share inside of the assembly) but aren't accessible from other assemblies.

    Regards,

    Yasser.


    Don't be stickler and wine with William Shakespeare after the solution :^)
    "And this our life, exempt from public haunt, finds tongues in trees, books in the running brooks, sermons in stones, and good in everything." William Shakespeare
    Monday, April 26, 2010 7:15 AM
  • For example:
    namespace ConsoleApplication1
    {
      public class IAmAccessibleForAll
      {
        public void IAmAccessibleForAll() { }
        private void IAmAccessibleForThisClassOnly() { }
        internal void IAmAccessibleForThisAssemblyClassesOnly() { }
      }
    
      internal class IAmAccessibleInThisAssemblyOnly
      {
        public void IAmAccessibleForAllButInThisAssemblyOnly() { }
        private void IAmAccessibleForThisClassOnly() { }
        internal void IAmAccessibleForThisAssemblyClassesOnly() { }
      }
    
      class IAmAccessibleInThisAssemblyOnly//like internal access modifier but they are same for classes only
      {
        public void IAmAccessibleForAllButInThisAssemblyOnly() { }
        private void IAmAccessibleForThisClassOnly() { }
        internal void IAmAccessibleForThisAssemblyClassesOnly() { }
      }
    }

    Sincerely,

    Yasser.


    Don't be stickler and wine with William Shakespeare after the solution :^)
    "And this our life, exempt from public haunt, finds tongues in trees, books in the running brooks, sermons in stones, and good in everything." William Shakespeare
    Monday, April 26, 2010 7:45 AM