locked
Protected and Protected Internal method

    Question

  • Hi,

    Can any one explain me what is the difference between protected and protected internal method?

    Saturday, March 17, 2007 12:52 PM

Answers

  • That should be "if you make it protected internal only the classes you made in the DLL *or* derived classes can access the member".

    "protected internal" in C# (and VB) means protected *or* internal, not protected and internal.

    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

    Saturday, March 17, 2007 2:33 PM
  • protected; derived types may access the member.

    protected internal; only derived types or types within the same assembly can access that member, so they need to be in the same Dynamic Link Library or an executable file.

    So if you make a DLL that has a protected member, someone can derive from a base class and directly access the member, if you make it protected internal only the classes you made in the DLL that are derived from the class can access the member.

    theTroll
    Saturday, March 17, 2007 2:09 PM

All replies

  • protected; derived types may access the member.

    protected internal; only derived types or types within the same assembly can access that member, so they need to be in the same Dynamic Link Library or an executable file.

    So if you make a DLL that has a protected member, someone can derive from a base class and directly access the member, if you make it protected internal only the classes you made in the DLL that are derived from the class can access the member.

    theTroll
    Saturday, March 17, 2007 2:09 PM
  • That should be "if you make it protected internal only the classes you made in the DLL *or* derived classes can access the member".

    "protected internal" in C# (and VB) means protected *or* internal, not protected and internal.

    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

    Saturday, March 17, 2007 2:33 PM
  •  David Anton wrote:

    That should be "if you make it protected internal only the classes you made in the DLL *or* derived classes can access the member".

    "protected internal" in C# (and VB) means protected *or* internal, not protected and internal.

    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


    Yes it is protected OR internal
    Sunday, March 18, 2007 1:43 AM
  •  

    Thursday, March 27, 2008 6:58 AM
  • Protected:

    you can access the protected method whenever you inherit the method from base class.

     

    Protected Internal:

    It will act as "Internal" with in same assembly. It will act as "protected" across the assembly.

     

    Example:

    --Assembly name as ClassLibrary1

    namespace ClassLibrary1

    {

    public class Class1

    {

    protected internal void ProInt()

    {

    }

    protected void Pro()

    {

    }

    internal void Inte()

    {

    }

    }

    public class Class3 : Class1

    {

    public Class3()

    {

    Console.WriteLine("class3");

    Inte();

    Pro();

    ProInt();

    }

    }

    public class class4

    {

    public static void Main()

    {

    Class1 c3 = new Class1();

    c3.ProInt();

    c3.Inte();

    //--Here we cant access Prtotected Method

    }

    }

    }

    -- Assembly name as ClassLibrary2

    namespace ClassLibrary2

    {

    public class Class5 :Class1

    {

    public void Main()

    {

    ProInt();

    Pro();

    //--Here we cant access internal method

    }

    }

    }

    Thursday, March 27, 2008 7:24 AM
  •   one more thing
     

    namespace ClassLibrary2

    {

    public class Class6

    {

    public void Main()

    {

    Class1 C1 = new Class1();
    //C1.ProInt();     
    //does NOT work like in Class4 above, not in the
    //same assembly and not derived

    }

    }

    }

    • Edited by bulaboard Sunday, June 29, 2008 3:06 PM fixed cr and lf
    • Proposed as answer by Ankur Bhutani Tuesday, August 07, 2012 12:49 PM
    • Unproposed as answer by Ankur Bhutani Tuesday, August 07, 2012 12:50 PM
    Sunday, June 29, 2008 2:57 PM
  • Thankss to karventhan and bulaboard for such a nice and pratical explanation,,,,fantastic job buddy,,,
    • Proposed as answer by RanjithAtMsdn Tuesday, October 06, 2009 9:22 PM
    Thursday, August 06, 2009 8:01 AM
  • Hi,

    Can any one explain me what is the difference between protected and protected internal method?                                                                                                                               

     

    These two are the access specifiers where by protected u mean that the members of the class which are protected will be inherited only by the class which inherits this class but in the case of protected internal they can be used out throughout the class.

    Tuesday, March 15, 2011 10:54 PM
  • Protected:

    you can access the protected method whenever you inherit the method from base class.

     

    Protected Internal:

    It will act as "Internal" with in same assembly. It will act as "protected" across the assembly.

     

    Example:

    --Assembly name as ClassLibrary1

    namespace

     

    ClassLibrary1

    {

     

     

    public class Class1

    {

     

     

    protected internal void ProInt()

    {

    }

     

     

    protected void Pro()

    {

    }

     

     

    internal void Inte()

    {

    }

    }

     

     

    public class Class3 : Class1

    {

     

     

    public Class3()

    {

     

     

    Console.WriteLine("class3");

    Inte();

    Pro();

    ProInt();

    }

    }

     

     

    public class class4

    {

     

     

    public static void Main()

    {

     

     

    Class1 c3 = new Class1();

    c3.ProInt();

    c3.Inte();

    //--Here we cant access Prtotected Method

    }

     

    }

    }

    -- Assembly name as ClassLibrary2

    namespace

     

    ClassLibrary2

    {

     

     

    public class Class5 :Class1

    {

     

     

    public void Main()

    {

    ProInt();

    Pro();

    //--Here we cant access internal method

    }

    }

    }


    The above example is really a good one, thanks to Karventhan, but I have a doubt after going thro' this coding..

     Here the protected internal member is accessed in both derived class of the same assembly and derived class of a different assembly. If this example is correct then why should we say that protected internal member can be accessed in a derived class "OR" classes in the same assembly?

    The "OR" in the above sentence doesn't make any sense in that and its confusing alot..

    The "OR" should be replaced with "AND" making it..protected internal member can be accessed in a derived class "AND" classes in the same assembly.

    I am quoting this becoz the derived can be in any assembly according to the example above, then why not the protected internal member can be accessed in a derived class AND classes in the same assembly?

    Correct me if I am wrong, I also want an accepting answer for this..

    Thanks.

     

    Wednesday, March 16, 2011 5:39 AM
  • The "OR" should be replaced with "AND" making it..protected internal member can be accessed in a derived class "AND" classes in the same assembly.

    A class must be derived or in the same assembly to access the member. The member can be accessed from derived classes and from classes in the same assembly. Choose your wording.
    Wednesday, March 16, 2011 11:08 AM
  • Mr.Louis did you see the complete conversation of this question?

    There is no difference in those two sentences which you said now but there is alot of difference between OR and AND..

    Mr.Dave and many of them stated as

    "protected internal" in C# (and VB) means protected *or* internal, not protected and internal."

    But this seems to differ when I see the example given by Mr.Karventhan and I dont find any fault in his example too..

    Wednesday, March 16, 2011 11:27 AM
  • Actually, that one sentence is correct: protected internal means protected OR internal accessibility. True if we're checking for protected access (from a derived class), true if we're checking for internal access (from a class in the same assembly).

    Think about "friend-or-family admittance".

    The sentence above that one is incorrect: "if you make it protected internal only the classes you made in the DLL *or* derived classes can access the member". It should be: "if you make it protected internal only the classes you made in the DLL and derived classes can access the member".

    Wednesday, March 16, 2011 12:53 PM
  • Actually, the meaning of "internal AND protected" vs "internal OR protected" differs based on if you see these access modifiers as restrictions (incorrect) or extensions (correct).

    If you view them as restrictions, it makes sense that "internal AND protected" is an even stronger restriction (it has to be in a derived class AND in the same assembly). Again, that is incorrect .

    Default access is "private" and all other access modifiers are extensions that allow the method/property to be accessed in additional situations. Because of that, "protected internal" methods/properties can be accessed from both dervied classes AND classes (which may or may not be derived) in the same assembly.

    Wednesday, March 16, 2011 2:10 PM
  • Because of that, "protected internal" methods/properties can be accessed from both dervied classes AND classes (which may or may not be derived) in the same assembly.


    This what I am trying to say from the beginning both derived classes (in the same assembly or different assembly) "AND" classes in the same assembly can access the members which is declared as "Protected internal".

     

    • Proposed as answer by DurgaMani Tuesday, April 12, 2011 8:26 AM
    Thursday, March 17, 2011 4:10 AM
  • One important thing is missing by all replies.

    For example if u define a method as:

     

     protected internal void DoSomething()
     {
     Console.WriteLine("I'm Doing...");
     }

    Normally, you think this method is protected and internal!!!! <---- THIS IS WRONG !!!!!

    The connection between them is OR  = protected internal means protected OR internal

    Example for that if you define an internal property and you try to set the setter or the getter as protected internal you will get a restrictive error  because The union of protected and internal accessibility is less restrictive than protected or internal alone.

     





    Thursday, April 14, 2011 9:11 AM
  • Karventan has given the right explanation in simple words...

    To explain the same without using AND or OR, i would say...

    Protected:

    ->Protected members can be accessed by any derived class in any assembly(DLL)

    Protected Internal:

    ->Protected Internal members can be accessed by all the Classes in the same assembly(DLL) whether they are derived or not derived. [No need to derive the base classes in the same assembly just to access the Protected Internal members].

    AND [this AND refers to simple English AND]

    ->Protected Internal members can be accessed by all the derived classes in the other assemblies.

    Monday, March 19, 2012 1:51 PM
  • Hi, thanks, some good explanations here,

    and DurgaMani, I think the confusion should be cleared up by a point well made by mahendarH above, concerning the lingustical versus logical difference between the linguistic words "and", "or" versus logical operators AND, OR.

    If we have protected internal method (x) being attempted to be called by (a) another class in the same assembly, (b) a derived class in a different assembly. They both work because... 

    linguistically, scenario (a) can can do it, "and" so can scenario (b).

    however logically scenario (a) AND scenario (b) cannot both be true at the same time, which is the definition of a logical AND

    so logically it's either scenario (a) OR scenario (b), they both work but logically OR-ed not AND-ed as that would be impossible.

    I think we all agree how it works from the good examples, and I also think it's easier to appreciate that by distinguishing logic/language and also try saying it the other way round... internal protected, rather than protected internal:

    Internal Protected = Internal to my own assembly for any class (derived or not) I'm fully internal, but if you're coming at me from another assembly - I'm not internal to you too, I'm protected to only my derivative classes in your unknown assembly.

    Edit: just noticed this seems a particular concern mostly around the months of March - June time in the past few years :)  



    Friday, July 13, 2012 12:39 AM