Hi
Can you override private virtual methods in C# ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
DarilPosted Nov 13, 2021, 5:31 AM
Jignesh KumarPosted Nov 2, 2021, 1:56 AM
Please refer this link which will help you to understand better,
Vinitha TPosted Nov 1, 2021, 4:14 PM
RanjithKumar chiguruvadaPosted Sep 24, 2012, 7:51 AM
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.
Satyapriya NayakPosted Sep 24, 2012, 7:37 AM
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
VulpesPosted Sep 24, 2012, 7:29 AM