locked
Hiding method using Property RRS feed

  • Question

  • User1904516115 posted

    Why child class property is not hiding base class method? I can use both by creating the object of child class?

    using System;
    
    namespace ConsoleApplication29
    {
    
        public class BaseClass
        {
            public void Member()
            {
    
            }
        }
    
        public class ChildClass : BaseClass
        {
            new public string Member { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                ChildClass child = new ChildClass();
                child.Member();//Method
                child.Member = "hello";//Property
            }
        }
    }
    

    Monday, September 5, 2016 5:49 PM

Answers

  • User753101303 posted

    I saw once that unless they have a strong benefit in adding a special behavior, language designers are just keeping the simplest behavior. So my guess is that the compiler just check the name without looking exactly if this is a method or whatever so "new" is always required. Still if you are using a method and a property it happens that the C# compiler is still able to make the difference and to call one or the other. I believe this kind of details should be in the C# specification.

    Edit: try perhaps https://msdn.microsoft.com/en-us/library/aa691135(v=vs.71).aspx for details. It seems to confirm that it doesn't look at the kind of member but just at the name (and the signature for methods).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 11, 2016 6:40 PM

All replies

  • User753101303 posted

    Hi,

    As the syntax is different and event though you can have a single member named this way, the compiler still have enough information to find without any problem if you are using the method or the property.

    If you want the full details, you'll likely find more details in the relevant part of the C# specification (try perhaps https://msdn.microsoft.com/en-us/library/aa645596(v=vs.71).aspx even though it's not the latest version).

     

    Monday, September 5, 2016 11:57 PM
  • User36583972 posted

    Hi vinodkpasi,

    According to your description , When a child class inherits the parent class, it inherits all the members of the parent class. According to your code, you created a new child class object , of course, also inherits the parent class.

    When you run project , The compiler will automatically recognize the different objects of the same name , this is a bit like a constructor.

    Link for your reference:

    Inheritance (C# Programming Guide) :

    https://msdn.microsoft.com/en-us/library/ms173149.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Yohann Lu

    Tuesday, September 6, 2016 7:02 AM
  • User1904516115 posted

    Hi PatriceSc,

    If the compiler still have enough information to find without any problem then why is showing compiler warning to use "new" keyword.

    Thanks
    vinod

    Saturday, September 10, 2016 7:07 PM
  • User753101303 posted

    I saw once that unless they have a strong benefit in adding a special behavior, language designers are just keeping the simplest behavior. So my guess is that the compiler just check the name without looking exactly if this is a method or whatever so "new" is always required. Still if you are using a method and a property it happens that the C# compiler is still able to make the difference and to call one or the other. I believe this kind of details should be in the C# specification.

    Edit: try perhaps https://msdn.microsoft.com/en-us/library/aa691135(v=vs.71).aspx for details. It seems to confirm that it doesn't look at the kind of member but just at the name (and the signature for methods).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 11, 2016 6:40 PM
  • User303363814 posted

    Don't do it.  The compiler is telling you that this is a confusing practice.  The compiler is right.

    Use a different name.

    Think of the next person to look at your code and imagine that they are looking at your code at 3AM to get a system running again and they are an axe murderer and they know where you live.

    PS - The next 'person' to look at your code might be yourself in six months time. Be nice to yourself.  Make your code as easy to understand as possible.  Use a different name.  You will appreciate it.

    Monday, September 12, 2016 1:33 AM