简体   繁体   中英

Calling a private base method from a derived class in C#

How can a derived class call a method from a base class.
Other classes should not have access on the other hand.

My situation:
I have a base class, in wich I wrote a private method to register some values.

private void register(string param1, int param2){//...}

I did this to allow subclasses to register different stuff.
The problem is, that a derived class cannot access private methods or fields of a base class.
That makes sense to me, since private means PRIVATE.

I don't want to make the method public because other classes should not be able to call this method.
Can someone provide a solution or guide me towards a better design?

When you declare something private, only the class that defines it can access it. Not even derived classes

What you need is protected

When something is declared as protected it can be accessed by any derived class while staying hidden from other non-related classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM