0

I understand that if I were to use a private access modifier, for example:

namepsace MyNameSpace
{
  class Book
  {
   private string title;
  }
}

Then this means that the instance variable title would only be accessible from within the class i.e the Book class in the above example.

However, if I were to make the instance variable public, I have read that this makes it accessible to outside of the class.

My question is, does this also mean it is accessible outside of the namespace? Or are we still talking about the classes within the namespace? I couldn't find any resource that answered this question of mine hence thought to ask here.

Blue
  • 22,608
  • 7
  • 62
  • 92
Baba.S
  • 324
  • 2
  • 14
  • 2
    Tag this with language that you are using to get the relevant folks to look at it. – Yogesh_D Aug 26 '16 at 12:10
  • 3
    It seems like the [Access Modifiers](https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx) reference would explain what you want to know. Always check the docs when you have a question about the language/framework. – mason Aug 26 '16 at 13:42

3 Answers3

2

Namespaces are purely for organization, not for accessibility. There are no accessibility mechanisms in .NET that allow/restrict access to resources based on namespace. Since namespaces can be spread across multiple assemblies, making a member private "to a namespace" would not be effective since anyone could write a class in a different assembly but the same namespace and access those members.

You can infer this from the documentation that makes no mention of namespace when discussion accessibility, only types and assemblies.

In general, private means "can only be used by the thing that contains the definition". For members, that's the class; for types, it's the "outer" type that defines it, if there is one. You can't have a top-level private type since namespaces aren't really "containers" so there's nothing to make it private to.

Other questions that address this:

Namespace only class visibility in C#/.NET?

private classes inside namespaces

How do you make infrastructure code only visible in namespace?

Community
  • 1
  • 1
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

Complete location of title is MyNameSpace::Book::title. Namespaces do not restrict the accessibility of its members. Namespace give new location to its members. class is also a type of Namespace.

For example:

In China there might be a telephone with number 123456789. Also in Singapore there might be a telephone with same number 123456789. Any one can dial these numbers. But to distinguish between these different numbers, we need Country Code and sometimes STD Code as well. So here, Country Code and STD Code are Namespace.

Making a member of class public won't affect it from getting accessible outside Namespace. However, the member access should be valid via object of class.

sameerkn
  • 2,209
  • 1
  • 12
  • 13
0

No. It's not about namespace. A namespace is a location. As long as your class is accessed, its' public members will be able to be accessed too.

Doruk
  • 884
  • 9
  • 25