Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override but don't call

How do you declare a method in C# that should be overridden (or overridable) by a dereived class - possibly even outside your assembly - but that should be callable only from within the actual class?

(i.e. like a private virtual function in C++)

[edit]
private virtual is exactly what I intend: "Here's a way to modify my behavior, but you are still not allowed to call this function directly (because calling it requires arcane invocations that only my base class shall do)"

So to clarify it: what is the best expression for that in C#?

like image 302
peterchen Avatar asked Dec 30 '22 06:12

peterchen


1 Answers

When you say it should only be callable "within the actual class" do you mean the base class or the derived class? Neither of these is feasible on its own. The closest is to use a protected method, which means it can be called from the declaring class, the derived class, and any further-derived class.

like image 60
Jon Skeet Avatar answered Jan 11 '23 23:01

Jon Skeet