Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inheritance of private members in c#

Are private members inherited when inheriting the class in c#? I have read some topic related to this, somebody telling that private members are inherited but cannot access the private members, somebody telling that it is not inherited when inheriting the class. Please explain the concept. if it is inheriting can any body give an explanation?

thanks

like image 525
tester Avatar asked Dec 11 '22 10:12

tester


1 Answers

If I understand your question correctly then you're not concerned about the accessibility you are only concerned about private members are inherited or not

Answer is yes, all private members are inherited but you cant access them without reflection.

public class Base
{
    private int value = 5;

    public int GetValue()
    {
        return value;
    }
}

public class Inherited : Base
{
    public void PrintValue()
    {
        Console.WriteLine(GetValue());
    }
}

static void Main()
{
    new Inherited().PrintValue();//prints 5
}
like image 165
Sriram Sakthivel Avatar answered Dec 13 '22 22:12

Sriram Sakthivel