1

I am organizing my unity project into namespaces, and want to hide the classes that are used only inside the namespaces. I define the namespace MyProject.Accounts and place an internal class there:

namespace ProjectName.Accounts
{
    internal struct PictureData
    {
        internal int notSeenMember;
    }
}

Then i add the using ProjectName.Accounts to a file in ProjectName.LoginScreen namespace, and i am able to see the class through the Intellisence, and instanciate it, but not to use the notSeenMember member:

The MS says that : Internal types or members are accessible only within files in the same assembly.

Is being able to instanciate internal classes from other namespaces an expected behavior?

EDIT: you can also access the internal members

TheSmokingGnu
  • 302
  • 2
  • 6
  • 15
  • 3
    Possible duplicate of [Namespace or Assembly?](https://stackoverflow.com/questions/21530460/namespace-or-assembly) – Zohar Peled Feb 08 '18 at 12:32
  • @ZoharPeled that questions helps differenciate between the two, whereas i don't see Assemblies playing any role in my question. Can you elaborate on how is it related? – TheSmokingGnu Feb 08 '18 at 12:37
  • 1
    They are related because `internal` is for the assembly, doesn't matter at all which namespaces are used in which assembly – Camilo Terevinto Feb 08 '18 at 12:39
  • 1
    Because `Internal` doesn't care about namespaces, only about assemblies. The fact is that you can access internal types or members from a different namespace within the same assembly. Your question suggests you have confused the two terms - *"Is being able to instanciate internal classes from other namespaces an expected behavior?"* **Yes**, as long as they both are in the same assembly. – Zohar Peled Feb 08 '18 at 12:40
  • Ah, i can see now, thank you. So there is no way to hide the class from others in the same Assembly? – TheSmokingGnu Feb 08 '18 at 12:41
  • 1
    Not by namespace, no. A private nested class would only be visible within the containing class though. – Jon Skeet Feb 08 '18 at 12:45
  • only by using `private` or `protected` (or `private protected`) - but I think that's more restricting that what you need. – Zohar Peled Feb 08 '18 at 12:46

0 Answers0