difference between internal and protected internal

Last post 07-02-2009 4:25 PM by kossovsky. 4 replies.

Sort Posts:

  • difference between internal and protected internal

    04-07-2009, 6:26 AM
    • Member
      36 point Member
    • kapilbharat
    • Member since 12-04-2007, 6:04 PM
    • Posts 82

     good evenning all,

     

    I want to what is difference between Internal and protected internal.

  • Re: difference between internal and protected internal

    04-07-2009, 7:23 AM
    • All-Star
      33,913 point All-Star
    • vcsjones
    • Member since 04-18-2006, 8:53 PM
    • Falls Church, VA
    • Posts 4,331
    • Moderator
      TrustedFriends-MVPs

     Internal means that only classes within the same assembly can access that type or member (with the exception of InternalsVisibleToAttribute).

    Protected means the member can only be accessed by a deriving type (child class accessing a super class).

    Protected internal is a combonation of both of them. It can only be accessed within the same assembly and it can only be accessed as a child class.

    Cheers,
           Kevin Jones


  • Re: difference between internal and protected internal

    04-07-2009, 10:19 AM
    Answer
    • Contributor
      2,394 point Contributor
    • David Anton
    • Member since 06-12-2005, 9:59 PM
    • Posts 431

    'protected internal' means 'protected or internal' - this means that it can be accessed within the same assembly or by a deriving type.

    e.g.,

    case 1: it can be accessed within the same assembly, by any type

    case 2: it can be accessed outside of the assembly, by a deriving type

    Note that C# (and VB) have no equivalent to 'protected and internal' where both aspects must be true.

    David Anton
    Convert between VB, C#, C++, & Java
    http://www.tangiblesoftwaresolutions.com
  • Re: difference between internal and protected internal

    04-08-2009, 12:04 AM

    kapilbharat:

     good evenning all,

     

    I want to what is difference between Internal and protected internal.

    for eg if u define some error constants in  a project and u want to use the error constants only to this project and no one else is allowed to access then u define the "error constants' class as "internal" since u dont want others to use.

    I am not sure of "protected interanl" and expecting any solid example to understand the scenerio

    Any ideas...

     

    Jai Ganesh. J , GSD ,India

    Please Mark As Answer If my reply helped you.
  • Re: difference between internal and protected internal

    07-02-2009, 4:25 PM
    • Member
      20 point Member
    • kossovsky
    • Member since 07-02-2009, 4:19 PM
    • Posts 5

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

    protected internal - Access is limited to the current assembly or types derived from the containing class.

    So, as you can see “protected internal” can be used in the same assembly or types derived from the containing class in any assembly.

    It means that we can access a “protected internal” method by accessing any instance of it’s class in the same assembly, in any class in different assembly which derives from the class, but we won’t be able to access the method by accessing an instance of it’s class in different assembly.


    // Assemby : A
    namespace AssemblyA
    {
        public class A
        {
            protected internal string SomeProtectedInternalMethod() {
                return "SomeValue";
            }
        }
    
        public class A2 : A
        {
            public string SomeMethod() {
                // We can access the method because
                // it's protected and inherited by A2
                return SomeProtectedInternalMethod();
            }
        }
    
        class A3 : A
        {
            public string SomeMethod()
            {
                A AI = new A();
                // We can access the method through an instance
                // of the class because it's internal
                return AI.SomeProtectedInternalMethod();
            }
        }
    }


    // Assemby : B
    using AssemblyA;
    namespace AssemblyB
    {
        class B : A
        {
            public string SomeMethod() {
                // We can access the method because
                // it's inherited by A2
                // despite the different assembly
                return SomeProtectedInternalMethod();
            }
        }
    
        class B2
        {
            public string SomeMethod()
            {
                A AI = new A();
                // We can't access the method
                // through the class instance
                // because it's different assembly
                return AI.SomeProtectedInternalMethod();
            }
        }
    }




    Regards,

    Visit my Blog - .NET Tips & Tricks
Page 1 of 1 (5 items)