Hi friends....
I know virtual methods but I want to know about private virtual methods ? Please explain with an example ?
Thanks,,,,,
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Datta KharadRank #2,135 · 668 reputationPosted Feb 7, 2012, 9:07 PM EST
Private virtual method:-
Private virtual methods cannot be declared in the C# language. In programming languages, polymorphism based on types is a way to virtualize the entry point to a data object; for private methods, you have already entered the data object, and thus no virtualized entry point is ever required.
In this example, we see both a program that attempts to use a private virtual method member, as well as the compile-time error that the program results in. As a reminder, the error occurs during the static compilation phase, which means that no program that declares a private virtual method will ever be executed in any way.
Example of private virtual method():_
class A
{
private virtual int Test()
{
return 1;
}
}
class Program
{
static void Main()
{
}
}
Compile-time error
Error 1
'A.Test()': virtual or abstract members cannot be private.
Notes. Virtual methods are an implementation of type-based polymorphism. When you have a derived class and a base class, the derived class can re-implement the base class virtual method. This gives you a dynamic entry point to the class type, which enables you to clearly separate the usage of the type from the implementation of each subtype.
Instance private methods. Whenever a private method is invoked, the type has already been entered—you can always define custom private methods on a type and if you are already inside the type, you can simply call a regular instance private method. Further, instance and static methods are faster to invoke than interface and virtual methods, so there is a performance advantage to not using a private virtual method.
Notice:
Private virtual methods are allowed in the C++ programming language. They are used in some design patterns that emphasize the importance of overriding certain pieces of an algorithm only.