user4081625
user4081625

Reputation:

Can internal classes be accessed within other namespaces?

I'm currently reading this book online: http://www.angelfire.com/theforce/chewy0/csharp/Thinking_in_C-Sharp_.pdf

On page 23 (38 of the PDF document) it states that internal classes cannot be accessed from classes within external namespaces.

However, in the online Microsoft documentation it states that internal classes are only accessible from the same assembly.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/internal

From my understanding, an assembly can contain classes from multiple namespaces. Would this not mean from the Microsoft documentation that internal classes could be accessed across different namespaces?

Or is it true to say that internal classes are private within both of their respected assemblies and namespaces?

Upvotes: 5

Views: 8084

Answers (2)

John Wu
John Wu

Reputation: 52210

So this is the excerpt, right?

Java uses five explicit keywords to set the boundaries in a class: public, private, protected, internal, and protected internal. Their use and meaning are quite straightforward. These access specifiers determine who can use the definitions that follow. public means the following definitions are available to everyone. The private keyword, on the other hand, means that no one can access those definitions except you, the creator of the type, inside member functions of that type. private is a brick wall between you and the client programmer. If someone tries to access a private member, they’ll get a compile-time error. protected acts like private, with the exception that an inheriting class has access to protected members, but not private members. Inheritance will be introduced shortly. internal is often called “friendly”–the definition can be accessed by other classes in the same namespace as if it were public, but is not accessible to classes in different namespaces. Namespaces will be discussed in depth in chapter #ref# [sic]. protected internal allows access by classes within the same namespace (as with internal) or by inheriting classes (as with protected) even if the inheriting classes are not within the same namespace.

C#’s default access, which comes into play if you don’t use one of the aforementioned specifiers, is internal

The author is probably conflating Java's internal with c#'s internal.

They are slightly different, because Java does not have assemblies; it has packages, which organize classes into namespaces.

In c#, namespace has absolutely no relationship with accessibility modifiers. Only classes within the same assembly can access an internal type or member, unless you use the InternalsVisibleTo attribute. Namespace doesn't matter.

Upvotes: 3

Shtut
Shtut

Reputation: 1407

Well, the easiest way to answer this was to test it- So I've made 2 namespaces within 1 assembly, and accessed an internal variable.

Short answer- the Microsoft documentation is correct- it's possible to access an internal variable within the same assembly, even when you have 2 different namesapces.

Here's the code:

namespace ConsoleApplication1
{
    class Class1
    {
        internal string thing;
        public Class1()
        {
            thing = "original data";
        }
    }
}

namespace ConsoleApplication2
{
    class Class2
    {
        public ConsoleApplication1.Class1 class1= new ConsoleApplication1.Class1();
        public Class2()
        {
            class1.thing = "other namespace modification";
        }
    }
}

When calling Class2 constructor, the modified data was displayed.

var class2 = new ConsoleApplication2.Class2();
Console.WriteLine(class2.class1.thing);

Result:

"other namespace modification"

Hope this helps :)

Upvotes: 2

Related Questions