简体   繁体   中英

Child class property available in base class object in c#

Here I have the following code in which I have created two class A and B. Then in the main method, I created the object of both the class and assign child object to parent object. I don't understand how it works in c# can anyone explain me?

 class Program
{
    static void Main(string[] args)
    {
        A objA = new A();
        B objB = new B();
        objA = objB;


        Console.ReadLine();
    }

}

public class A
{
    public string ABC { get; set; }

    public string XYZ { get; set; }

    public string lmn { get; set; }


}

public class B : A
{
    private string vvmdn { get; set; }

    public string mkkk { get; set; }

}

在此处输入图片说明

The reference objA points to a B object in memory and the debugger shows all properties of this object.

You can access non-public members of an object at runtime yourself using reflection: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection . This is basically what the debugger in Visual Studio does.

The type of the reference objA is indeed A but the actual object that it points to in memory is a B .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM