Confusión: interno, protegido y protegido interno [duplicado]

Posible duplicado:
¿Cuál es la diferencia entre 'protegido' e 'interno protegido'?
¿Cuál es la diferencia entre público, privado, protegido y nada?

Code is as mentioned below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testanotherlib
{
    public class A
    {
        internal void InternalDisplay()
        {
            Console.WriteLine("Internal Display Method.");
        }

        protected void ProtectedDisplay()
        {
            Console.WriteLine("Protected Display Method.");
        }

        protected internal void ProtectedInternalDisplay()
        {
            Console.WriteLine("ProtectedInternal Display Method.");
        }

        public void PublicDisplay()
        {
            Console.WriteLine("Public Display Method.");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testanotherlib
{
    public class B : A
    {
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testanotherlib;
namespace testlib
{
    public class C:A
    {
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using testlib;
using testanotherlib;

namespace testapp
{
    class Program
    {
        static void Main(string[] args)
        {
            B objB = new B();
            C objC = new C();
        }
    }
}

I am trying to understand the difference between Internal, Protected and Protected Internal. For that I have created an example using the code above.

In a class library project testanotherlib I have class A & class B. In a class library project testlib I have class C. The program class is in a separate console application. Inside the main method of Program class I have created object for class B (objB) and class C (objC). For objB and and objC only the public method of class A are accessible. I was expected for class B all the methods of class A will be accessible. Kindly help me to understand this. If you need any other information about the project, feel free to ask me.

Saludos, Priyank

preguntado el 10 de marzo de 12 a las 14:03

Where were you expecting to be able to access all the methods of class A, with a reference to a class A? Your code never tries to utilizado the members, which makes it hard to talk about... -

@JonSkeet: I was expecting to be able to access all the methods if class A, with reference objB. -

@PriyankThakkar: From testApp? Por qué were you expecting that? The code in testApp isn't in the same assembly as A so any internal members aren't visible, for example. -

Why doesn't Microsoft provide a topic on PROTECTED INTERNAL in MSDN? -

3 Respuestas

The following five accessibility levels can be specified using the access modifiers:

público: el acceso no está restringido.

protegido: el acceso está limitado a la clase contenedora o los tipos derivados de la clase contenedora.

Internal: Access is limited to the current assembly.

protected internal: el acceso está limitado al ensamblado actual o los tipos derivados de la clase contenedora.

private: Access is limited to the containing type.

Taken directly from Microsoft's MSDN library.

respondido 10 mar '12, 14:03

The definition you give for 'protected internal' seems inaccurate because of 'or'. Are you sure that it is not supposed to be 'and'? - ciuncan

You might be right. Please inform Microsoft immediately!! - Chris Gessler

Oh, I didn't notice the link, and was lazy to search. Sorry. And still seems rather logical. - ciuncan

internal

Only visible in the current and friendly assemblies.

protected

Only visible within classes that inherit A.

protected internal

Visible within classes that inherit A. And also visible within the current and friendly assemblies.

respondido 10 mar '12, 14:03

hey simon.. thanks for the help :) - Priyank Thakkar

protected internal means protected OR internal; you described it as protected AND internal. - Tuan

protegido methods and members can only be accessed from another Class that derives from the class declaring the procted method.

class A 
{
    protected void Method() {}
}

class B : A
{
    public void Foo()
    {
        Method(); // works!
    }
}

class C 
{
    public void Foo()
    {
        Method(); // won't work, obviously

        var tmp = new A();
        tmp.Method(); // won't work either because its protected
    }
}

interno makes the method only visible in the same assembly. For classes in the same assembly the method can be used like it were public. for classes outside of your current assebmly its like private.

Now combining protected and internal makes a method usable in the same assembly for all classes in that assembly. And the protected makes the method usable in all derived classes no matter which assembly.

respondido 10 mar '12, 15:03

You got protected internal wrong, see my answer or Chris' for that matter. - Terkel

@SimonBangTerkildsen Oh you are right. i corrected my answer. Didn't knew that. Well, never used it myself. So internal effectivly overrules the protected keyword. And the internal is ignored when derived. Didn't knew that, and in fact didn't like it :) But there must be a good reason for this. - hacer mientras

@dowhilefor: thanks for explaining it with wonderful example :) :) - Priyank Thakkar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.