Transparency of visibility only namespace in C # /. NET?

In C #, can you make a class visible only inside its own namespace without living in another assembly? This seems useful for regular helper classes that should not be used elsewhere. (i.e., java calls private package classes)

+63
c #
Aug 03 '09 at 18:25
source share
5 answers

I do not think that what you want is possible.

+25
Aug 03 '09 at 18:43
source share

You can create internal classes, but this does not allow anyone other than the assembly to use the class. But you still have to make a separate assembly for each namespace with which you want to do this. I guess that is why you would not want to do this.

Getting a C # Compiler to Ensure Namespace Visibility

Here's an article ( namespace visibility in C # ) that shows a method for using partial classes as a form of a "fake namespace" that may be useful to you.

The author points out that this does not work perfectly, and he discusses the flaws. The main problem is that C # developers developed by C # do not work this way. This is very different from the expected encoding methods in C # /. NET, which is one of the main advantages of the .NET Framework.

This is a neat trick & hellip; now don't do it.

+57
Aug 03 '09 at 18:35
source share

internal is an assembly (strictly speaking, a module). This does not affect the visibility of the namespace.

The only way to achieve the confidentiality of a class from other classes within the same assembly is through a class that will be an inner class.

At this point, if the class is private, it is invisible to anything other than this class or the outermost class itself.

If the protection is protected, it is visible to everyone who can see it when it is private, but also visible to subclasses of the outer class.

 public class Outer { private class Hidden { public Hidden() {} } protected class Shady { public Shady() {} } public class Promiscuous { public Promiscuous() {} } } public class Sub : Outer { public Sub():base() { var h = new Hidden(); // illegal, will not compile var s = new Shady(); // legal var p = new Promiscuous(); // legal } } public class Outsider { public Outsider() { var h = new Outer.Hidden(); // illegal, will not compile var s = new Outer.Shady() // illegal, will not compile var p = new Outer.Promiscuous(); // legal } } 

In fact, the only way to achieve what you want is to use the outer class as a form of namespace and restrict yourself to this class.

+19
Aug 03 '09 at 18:40
source share

If you have one assembly, you can define as many namespaces in this assembly as you want, but no matter what modifier you use in the IDE, you can always see classes in other namespaces.

0
Aug 03 '09 at 18:55
source share

Not sure if this is possible, but some good ways to fake it:

1) Let the classes by which this type of material is inherited from one class that has an auxiliary class as an inner class.

2) Use extension methods, and then only refer to extension methods in the namespace.

0
Aug 03 '09 at 19:19
source share



All Articles