The C-sharp subclass only overrides the properties of the parent class, so why can't you get the overridden value when calling the method?

The

code is as follows, mainly looking at the last

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpClassTest
{
    /*
     * :https://blog.csdn.net/tc_1337/article/details/80957140-sharpcommentsedit
     */

    class A
    {
        public static string StaticStr = "A ";
        public string NonStaticStr = "A ";

        public string NoStaticProperty { get; set; } = "A ";

        public static void StaticMethod()
        {
            Console.WriteLine("A ");
        }

        public void NonStaticMethod()
        {
            Console.WriteLine($"A :A  | A :{NonStaticStr} | {NoStaticProperty}");
        }

    }

    class B : A
    {

        public static string StaticStr = "B ";
        public string NonStaticStr = "B ";

        public static void StaticMethod()
        {
            Console.WriteLine("B ");
        }

        public void NonStaticMethod()
        {
            Console.WriteLine($"B :{NonStaticStr}");
        }

    }

    class C : A
    {

    }

    class D : A
    {
        public string NonStaticStr = "D ";
    }

    class E : A
    {
        public string NoStaticProperty { get; set; } = "E ";
    }

    class Program
    {
        static void Main(string[] args)
        {
            C c = new C();
            Console.WriteLine(" AC :");
            Console.WriteLine(c.NonStaticStr);      // A 
            c.NonStaticMethod();                    // A :A  | A 
            Console.WriteLine("-------------------------------\r\n");

            A c1 = new C();
            Console.WriteLine(" CA :");
            Console.WriteLine(c1.NonStaticStr);     // A 
            c1.NonStaticMethod();                   // A :A  | A 
            Console.WriteLine("-------------------------------\r\n");

            B b = new B();
            Console.WriteLine(" AB :");
            Console.WriteLine(b.NonStaticStr);      // B 
            b.NonStaticMethod();                    // B :B 
            Console.WriteLine("-------------------------------\r\n");

            A b1 = new B();
            Console.WriteLine(" BA :");
            Console.WriteLine(b1.NonStaticStr);     // A 
            b1.NonStaticMethod();                   // A :A  | A 
            Console.WriteLine("-------------------------------\r\n");

            D d = new D();
            Console.WriteLine(" AD :");
            Console.WriteLine(d.NonStaticStr);     // D 
            d.NonStaticMethod();                   // A :A  | A 
            Console.WriteLine("-------------------------------\r\n");

            E e = new E();
            Console.WriteLine(" AE :");
            Console.WriteLine(e.NoStaticProperty); // E 
            e.NonStaticMethod();                   // A :A  | A 
            Console.WriteLine("-------------------------------\r\n");
        }
    }
}

inspired by @ Woody :

    class AA
    {
        public virtual string NoStaticProperty { get; set; } = "AA ";

        public void NonStaticMethod()
        {
            Console.WriteLine($"AA : :{NoStaticProperty}");
        }

    }

    class BB : AA
    {
        public override string NoStaticProperty { get; set; } = "BB ";
    }

    class CC : AA
    {
        public new string NoStaticProperty { get; set; } = "CC ";
    }
    
    //...
    
BB bb = new BB();
Console.WriteLine(" AABB :");
Console.WriteLine(bb.NoStaticProperty); // BB 
bb.NonStaticMethod();                   // AA : :BB 
Console.WriteLine("-------------------------------\r\n");

CC cc = new CC();
Console.WriteLine(" newAACC :");
Console.WriteLine(cc.NoStaticProperty); // CC 
cc.NonStaticMethod();                   // AA : :AA 
Console.WriteLine("-------------------------------\r\n");

AA ab = new BB();
Console.WriteLine(" BBAA :");
Console.WriteLine(ab.NoStaticProperty); // BB 
ab.NonStaticMethod();                   // AA : :BB 
Console.WriteLine("-------------------------------\r\n");

the example written by the owner of the building is a good example of what values will be obtained for several uses of these four classes.

landlords first need to figure out the concept of rewriting , and then look at these examples.
in fact, the method properties you define have nothing to do with inheritance.

first of all, in this example below, if you define a B, then any value you get will be the value of B.

B b = new B();

secondly, in the following example, if you define an A, then any value obtained will be the value of A, regardless of whether its instance is B. Because you didn't rewrite anything about An in B.

A b1 = new B();

you want BCDE to override the value of parent class A, then you need to override them. You should write something like this:


public class A 
{
    public virtual void NonStaticMethod() 
    {
        Console.WriteLine("A");
    }
}

public class B : A 
{
    public override void NonStaticMethod()
    {
        Console.WriteLine("B");
    }
}
Menu