Hi
Can you override private virtual methods in C# ?
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
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 1, 2021, 9:56 PM EDT
Please refer this link which will help you to understand better,
VulpesRank #55 · 29.2K reputationPosted Sep 24, 2012, 3:29 AM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Sep 24, 2012, 3:37 AM EDT
No, moreover, you cannot access private methods in inherited classes,They have to be protected in the base class to allow any sort of access.
http://stackoverflow.com/questions/2456222/can-you-override-private-virtual-methods
Thanks
RanjithKumar chiguruvada133 reputationPosted Sep 24, 2012, 3:51 AM EDT
You can't declare private virtual methods because private access specifier does not allow to inherit the private member of a class in a derived class.
for example,
using System;
public class ABC
{
private virtual void mm() { }
}
public class abc:ABC
{
private override void mm()
{
Console.WriteLine("Hello");
}
}
class d:abc
{
public static void Main()
{
d D=new d();
D.mm();
}
}
Error:
------ 'ABC.mm()' : virtual or abstract members cannot be private 'abc.mm()' : virtual or abstract members cannot be private
But you can override protected virtual methods.
Vinitha TRank #8,005 · 230 reputationPosted Nov 1, 2021, 12:14 PM EDT
Daril148 reputationPosted Nov 13, 2021, 12:31 AM EST