4

I want to seal of classes in a namespace. I was looking at the "internal" access modifier, but this only seems to seal of classes in an assembly. Can I seal of classes in a namespace?

Or do I have to move stuff into an seperate assembly? But then I will have the problem of visual studio refusing circular assembly references.

3 Answers3

3

No, there is no namespace-specific modifier. One option would be to use inheritance and a "protected" modifier, but having an internal constructor on the base-class so that external code can't subclass it. That might help.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    ...after all if there was a way to restrict to the namespace, people would still be able to define the same namespace in their code and inherit that way... – Rowland Shaw Jan 19 '09 at 12:34
2

It is not possible with C#.

Namespace-level members can only be either public or internal

You can however, use nested class in C#

namespace A {
    public class B {

        protected class C { }
    }

    public class D {

        void E() {
            var F = new A.B();    // ok!
            var G = new A.B.C();  // error!
        }
    }
}
chakrit
  • 61,017
  • 25
  • 133
  • 162
0

You can still use the internal keyword and expose the internal classes to another assembly via the InternalsVisibleTo attribute in the AssemblyInfo.cs file.

Patrik Svensson
  • 13,536
  • 8
  • 56
  • 77