If I declare an inner class, what should be the default access level for inner members?

I am creating a DLL with some basic functionality. In short, I am making several static classes for developers to use. These classes use some of the other classes that do the dirty work, which I marked as internal because I don't want people to access them.

Question: if I declare a class to be internal, what will be the access level of its members?

Do I have to mark all its members as internal, or are they automatically marked as internal too?

It's a good 2 hours. I do some googling and googling on stackoverflow and I'm struggling to find a clear and straightforward answer that doesn't include 1000 speculations, a technical not-so-plausible hypothesis, and useless embellishments ...

MSDN is confusing as usual (never found a clear answer on msdn).

From what I can read here http://msdn.microsoft.com/en-us/library/ms173121.aspx I assume that no matter how you set the access level of the class, all its members will be private (methods , variables, etc.).

Help i don't know

+3


source to share


5 answers


Question: if I declare a class to be internal, what level of access will its members be?

The default will be private. If anything else, then it depends. If they are anything other than public, then the access modifier is applied as described on MSDN (not visible outside of assembly, for example).

However, in the link you posted, there is one getcha that applies to non-static classes:

Typically, the accessibility of an element is no greater than the accessibility of the type that contains it. However, a public member of an inner class can be accessed from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in the public base class.


With regard to the last paragraph , since static classes cannot implement interfaces or inherit from other classes , then you can be sure. As long as you declare your static class to be internal, the members will not be available in other assemblies (unless your developers use reflection).

To demonstrate how it does the job for non-static classes:



Build 1

public interface ISomePublicInterface
{
    int GetValue();
}

internal class InternalClass : ISomePublicInterface
{
    public int GetValue()
    {
        return 100;
    }
}

public static class SomeFactory
{
    public static ISomePublicInterface GetInternalInstanceAsInterface()
    {
        return new InternalClass();
    }
}

      

Build 2

ISomePublicInterface val = SomeFactory.GetInternalInstanceAsInterface();
Console.WriteLine(val.GetValue()); //-->> Calls public method in internal class
Console.WriteLine(val.GetType());

      

Guess what is the way out?

Assembly1.InternalClass

      

So now you have access to the type outside the assembly and through reflection someone can call other internal methods (this is not the only way to get it).

+8


source


From MSDN only

The access level for class members and structure members, including nested classes and structures, is private by default.


the default is internal access.



Hope this table helps:

Members of    Default member accessibility
----------    ----------------------------
enum          public
class         private
interface     public
struct        private

      

Also check this MSDN

+3


source


Private unless otherwise noted. However, the audience will have the same result as the internal one.

If you subsequently promote the class from internal to public, then the creation of public class objects will become visible, while the methods within the country will remain internal.

You might want to consider the behavior in case of updating the scope of your class.

Another question.

+2


source


If u Declares a class as "internal" then you can access that class in the same assembly. But which access specifier you use for a class member decides that they are not available in another class in the same assembly.

+1


source


All members of the inner class will be internal and will be available in the same assembly and will not be outside of either the class or members.

If you want to access the class in other assemblies, make the class public, and the member you don't want to access the outer assembly will make them internal.

0


source







All Articles