tags:

views:

197

answers:

6

Suppose assembly Assembly1.dll contains 3 classes: Class C1, C2, C3.

I want to expose only class C1 to the outside world. Classes C2 and C3 will not be accessible. How to acheive this?

Note: Making classes C2 and C3 private is not an option as this will make them unaccessible inside the assembly itself.

+14  A: 

Make classes C2 and C3 internal, as in:

internal class C2
{
//...
}
Vojislav Stojkovic
+4  A: 

The "internal" keyword specifies that a class is accessible only within its own assembly. Perhaps you should tag C2 and C3 with this.

mquander
A: 

You can use "internal" accessor instead of "private" so your classes visibility will be confined to assembly level and not outside.

Stefano Driussi
+5  A: 

As others have said, you use internal visibility. A couple more points though:

  • For non-nested types, internal is actually the default in C#, so you don't have to specify it. Whether or not you explicitly specify the default access is a matter of personal taste. (I'm currently on the fence, leaning towards being explicit.)

  • Only nested types can be private in the first place - and again, that's the default visibility for nested types.

Jon Skeet
being explicit is good ...
Frederik Gheysels
Wasn't public the default in .NET 1.* ? To me, that in itself is a perfect reason for being explicit...
Carl
A: 

internal is the way to go. If you want to test your classes C2 and C3 from a different assembly, you can do this by setting the InternalsVisibleTo attribute on your assembly.

Brian Rasmussen