C # - Class Constructors and Access Modifiers

Given the following:

public abstract class Base
{
  // other stuff

  public static void StaticMethod()
  {
    PrivateMethod();
  }
  // here should be PrivateMethod() declaration somehow
}
public sealed class Derived: Base
{
  // other stuff

  public void InstanceMethod()
  {
    // call somehow PrivateMethod 
    PrivateMethod(); 
  }
}

I need to use PrivateMethod () from two different contexts (different assemblies). A call Base.StaticMethod()and a second time using an instance of the Derived class d.InstanceMethod();.

I am looking for a way to develop PrivateMethod () inside a base class. Of course, PrivateMethod () should not be visible outside the Base and Derived classes.

I was thinking something about "protected static PrivateMethod () {}", but I read that I shouldn't do this ...

What do you recommend to guys?

+3
source share
3 answers
  protected static void PrivateMethod() {}

OK (separate from the name) and does what you need. You will not need it base.when calling from Derived.

+2
source

, , , . : : . .

, , . , , , , -, .

, , , . , . , . , , " ", " , ".

+2

StaticMethod() InstanceMethod()... PrivateMethod() . PrivateMethod() . :

public abstract class Base
{
  // other stuff

  public static void StaticMethod()
  {
    PrivateMethod();
  }

  // here should be PrivateMethod() declaration somehow
  private static void PrivateMethod()
  {
    // do stuff
  }
}
public sealed class Derived: Base
{
  // other stuff

  public void InstanceMethod()
  {
    // call somehow PrivateMethod 
    StaticMethod(); 
  }
}

PS: StaticMethod ( InstanceMethod), .

0

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


All Articles