locked
Difference between Internal and Protected Internal

    Question

  • Hi All,
      What is the Difference between Internal and Protected Internal?
      I need some real time example.

    Regards
    Gomaz

    Monday, April 16, 2007 11:10 AM

Answers

  • 'protected internal' does not mean protected *and* internal - it means protected *or* internal.  So a protected internal member is visible to any class inheriting the base class, whether it's in the same assembly or not.  The member is also visible via an object declared of that type anywhere in the original assembly.

     

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: C# to Python converter, VB to Python converter
    Monday, April 16, 2007 11:51 PM
  • First you need to understand the meaning of each access modifier on its own:

    Protected:
    A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

    Internal:
    Internal types or members are accessible only within files in the same assembly.

    This means that a "procted internal" member (combination of these two) will be visible only to classes that derive from the class that declares that member *and* are declared in a file in the same assembly.

    (http://msdn2.microsoft.com/en-us/library/wxh6fsc7(VS.71).aspx)
    Monday, April 16, 2007 9:29 PM

All replies

  • Monday, April 16, 2007 12:06 PM
  • Hi ,

        When Protected Internal member becomes  protected *or* internal, not protected and internal?
      When can we use that member as Protected *or* internal?.
      or
      At What scenario we can use this access specifier?

      If you could give the some real time examples that would be more helpfull for me.

    Regards
    Gomaz
    Monday, April 16, 2007 1:07 PM
  • First you need to understand the meaning of each access modifier on its own:

    Protected:
    A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

    Internal:
    Internal types or members are accessible only within files in the same assembly.

    This means that a "procted internal" member (combination of these two) will be visible only to classes that derive from the class that declares that member *and* are declared in a file in the same assembly.

    (http://msdn2.microsoft.com/en-us/library/wxh6fsc7(VS.71).aspx)
    Monday, April 16, 2007 9:29 PM
  • 'protected internal' does not mean protected *and* internal - it means protected *or* internal.  So a protected internal member is visible to any class inheriting the base class, whether it's in the same assembly or not.  The member is also visible via an object declared of that type anywhere in the original assembly.

     

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: C# to Python converter, VB to Python converter
    Monday, April 16, 2007 11:51 PM
  • mmm...I may be confussed, but this is what I understand...

    protected -> the class itself and any inheriting class can access this method or propetry


    internal -> ANY class within same assembly can access this method/property, meaning any class within the same VS Project, exe, or dll can access it, whether it's inheriting it or not can get to it


    protected internal -> the class itself (where it's declared), the inheriting class(es), and just like internal, it can be access throughout our assemby by any class or part of assembly

     

    based on the above, what is the real need for "protected internal"?  If I want a method to be accessible by the base class, all inheriting classes, and throughout the assembly as well, then I can just declare it internal, why dcelare it protected internal?  what does it gain me, other than the extra keyword?

     

    Thanks in advance!

    Thursday, May 03, 2007 1:51 PM
  • If you just use 'internal', then it's not available outside of your assembly.

    If you just use 'protected', then it's not available to other classes in your assembly that aren't inheriting the class.

     

    'protected internal' allows the member to be available anywhere in the assembly in addition to any classes inheriting the class (in the assembly or not).

     

     

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter, VB to C++ converter
    Instant Python: C# to Python converter, VB to Python converter
    • Proposed as answer by zybernau Thursday, June 19, 2008 7:17 AM
    Thursday, May 03, 2007 3:01 PM
  • More exactly protected internal can be accessed:
    - from anywhere in the current assembly,
    - from any classes inheriting the class,
    - from any assembly declared as InternalsVisibleTo in the current assembly,
    - can be called using reflexion from any assembly that uses reflexion on the current assembly.
    Thursday, May 03, 2007 3:34 PM
  • thanks!  that's  the scenario I didn't think of, and where I got lost, so basically protected internal would have the same effect as internal on the same assembly, but on external assemblies, when we inherit the parent class we get to see it, where as if we only have internal, the external inheriting class won't see the method...thanks a lot, that clarifies it.
    Thursday, May 03, 2007 5:35 PM
  • The answer post is incorrectly marked.  The correct answer, as shown in the posts by others, is that a "protected internal" member is visible to classes that derive from the class that declares that member *OR* classes that are declared in a file in the same assembly.  Please remark the answer post so that people are not confused (David Anton's posts are the correct answers).  Thanks.
    • Proposed as answer by ChrisLaMont Friday, January 29, 2010 11:00 PM
    Tuesday, November 27, 2007 3:30 PM
  • Hi All,
      What is the Difference between Internal and Protected Internal?
      I need some real time example.

    Regards
    Gomaz


    A Internal member can be accessed within the namespace only.
    "Protected Internal" consists of Protected and Internal. It means "Protected Internal" members can be accessed as a Internal member within the assembly and as a Protected outside the assembly.
    You can check it by defining a variable as a "Protected Internal" and access it from another namespace.

    Regards
    PKSDotNet

    Saturday, June 13, 2009 12:48 PM
  • Behavior of the Access modifiers such as Public, Internal, and Protected Internal are same in the assembly level. These members can be accessed by all the clients in the same assembly.

    The Major difference is that, accessibility with outside the assembly.

    Public: Members can accessed by all the clients from outside the assembly.

    Internal: The internal members cannot be accessed by the clients outside the assembly.

    Protected Internal: These members can be accessed only by the clients who derives the class where the Protected Internal member defined.

    ----assembly1.dll

     namespace accessmodifiers {

             class baseClass{

                   protected Internal void proInternalFunction() {

                   //Function Definition.... }

                  Internal void InternalFunction(){

                  // Function Devinition...        }

            }

    ----assembly2.dll <add the reference of the assembly1.dll>

    namespace assembly2{

          class derivedClass : baseClass {

                 public void callProInternalFunction() {

                           proInternalFunction();

                }

                public void callInternalFunction() {

                          InternalFunction() // Compiler gives Error, since it doesnot have the accessiblity.

                }

          }

    }

    I hope this helps you!

    Thanks,

    Senthil

     

     

     

    Friday, March 26, 2010 7:37 PM
  •    
    public Access is not restricted.
    protected Access is limited to the containing class or types derived from the containing class.
    internal Access is limited to the current assembly.
    protected internal Access is limited to the current assembly or types derived from the containing class.
    private Access is limited to the containing type.

    Tuesday, June 14, 2011 6:30 PM
  • any class can be internal and protected at the same time?
    Tuesday, July 26, 2011 5:20 PM
  • protected internal can be used in  another assembly also.. 

    you can see below code.

     

    Project 1 

     

     

    using System;

    using System.Collections.Generic;

    using System.Text;

     

    namespace InternalAssembly1

    {

        public class Program

        {

            static void Main(string[] args)

            {

                Console.WriteLine("Hi Assembly1");

                Console.ReadLine();

               

                Kitty obj = new Kitty();

                obj.MakeSomeNoise();

                Console.ReadLine();

            }

        }

        public class Kitty

        {

            protected internal virtual void MakeSomeNoise( )

            {

     

                Console.WriteLine("This is Assembly 1, I am in Internal");

                Console.ReadLine();

             

            }

        }

    }

     

     

    Create the DLL for this and add the refrence in new project (Project 2)

     

     

    Project2

     

    using System;

    using System.Collections.Generic;

    using System.Text;

     

    namespace InternalAssembly2

    {

        using InternalAssembly1;

     

        public class Lion : Kitty

        {

            static void Main(string[] args)

            {

                Console.WriteLine("Hello Assembly 2");

              

                Lion objLion = new Lion();

                objLion.MakeSomeNoise();

                Console.ReadLine();

            }

            protected override void MakeSomeNoise()

            {

                Console.WriteLine("I am in Assembly 2 !!");

               

            }

        }

    }

    Tuesday, November 29, 2011 5:04 PM
  • You cannot inherit any class in other assembly without making it public.

    As you have used internal class in assembly1.dll and so it will give compile time error

    Thanks,

    Ankur

    Monday, August 06, 2012 7:34 AM
  • You cannot inherit any class in other assembly without making it public.

    As you have used internal class in assembly1.dll and so it will give compile time error

    Who said the class wasn't public?  You can have a protected internal member in a public class, which is exactly what the examples here have done.
    Monday, August 06, 2012 4:23 PM