Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Class design & 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 make use of PrivateMethod() from 2 different contexts (different assemblies). Once calling Base.StaticMethod(), and the second time by using an instance of the Derived class d.InstanceMethod();.

I am looking for a way how to design PrivateMethod() inside the 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 I should not do that...

What do you recommend guys?

like image 330
Learner Avatar asked Nov 30 '25 15:11

Learner


1 Answers

  protected static void PrivateMethod() {}

Is OK (apart form the name) and does what you require. You won't need base. when calling it from Derived.

like image 134
Henk Holterman Avatar answered Dec 03 '25 03:12

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!