Protecting a class in a namespace

In the namespace, I have a utility class that only works with the main class that I am viewing. How can I hide this?

+3
source share
5 answers

Make your class internal and work with different assemblies or create a private nested class of your main class.

public class A
{
  private class B { ... }
  ...
}
+10
source

If it will be used only internally - inside this "main class" - then it would be most suitable for its nesting.

In other words, your structure will look like this:

public class MainClass
{
    // ...

    private class NestedUtilityClass
    {
        // ... 
    }
}

, MainClass. protected, , , .

+4

Guillaume Noldorin anwsers - , . 2 - , - .

+3

, :

public class MyPublicClass
{
    private class PrivateClass
    {
    }
}
0

, . , . , , .

#:

. , , type - :

  • A nested type declared in a class can have any of five forms of declared accessibility (public, protected internal, protected, internal or private) and, like other members of the class, the default for private is declared accessibility.
  • A nested type declared in a structure can have any of three forms declared accessibility (public, internal or private) and, like other members of the structure, default values ​​for private declared accessibility.
0
source

Source: https://habr.com/ru/post/1713318/


All Articles