What is Shadowing?

2019-01-03 05:57发布

In C# what does the term shadowing mean? I have read this link but didn't fully understand it.

11条回答
女痞
2楼-- · 2019-01-03 06:43

Overriding: same name and exactly the same parameters, implemented differently in sub classes.

  • If treated as DerivedClass or BaseClass, it used derived method.

Shadowing: same name and exactly the same parameters, implemented differently in sub classes.

  • If treated as DerivedClass, it used derived method.
  • if treated as BaseClass, it uses base method.
查看更多
Evening l夕情丶
3楼-- · 2019-01-03 06:48

Suppose I have a base class that implements a virtual method:

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

I also have a derived class that also defines a method M:

public class B : A
{
    // could be either "new" or "override", "new" is default
    public void M() { Console.WriteLine("In B.M()."); }
}

Now, suppose I write a program like this:

A alpha = new B(); // it's really a B but I cast it to an A
alpha.M();

I have two different choices for how I want that to be implemented. The default behavior is to call A's version of M. (This is identical to the behavior if you applied the "new" keyword to B.M().)

This is called "shadowing" when we have a method with the same name but a different behavior when called from the base class.

Alternately, we could have specified "override" on B.M(). In this case, alpha.M() would have called B's version of M.

查看更多
Fickle 薄情
4楼-- · 2019-01-03 06:48

Shadowing isn't something I'd be worried about understanding or implementing unless it "fits" the problem really well. I've seen it used improperly and cause weird logic bugs much more often than being used correctly. The big cause, I think, is when the programmer forgets to put overrides in a method signature then the compiler warning will suggest the new keyword. I've always felt that it should recommend using override instead.

查看更多
看我几分像从前
5楼-- · 2019-01-03 06:49

Shadowing consist on hiding a base class method with a new definition in a child class.

The difference between hiding and overriding has to do with the way methods are invoked.

That way, when a virtual method is overridden, the call address for the method call table of the base class is replaced with the address of the child routine.

On the other hand, when a method is hidden, a new address is added to the method call table of the child class.

When a call is made to the method in question:

  1. The method call table class type is obtained, if we are invoking with a reference to the base class then the base class method table is obtained, if we have a reference to the child class, then the child class method table is obtained.
  2. The method is searched in the table, if it's found then the invocation takes place, otherwise the base class method table is searched.

If we invoke the method with a reference to the child class then the behavior is the same, if the method has been overridden, the method address will be found in the base class, if the method was hidden the method address will be found on the child class, and since it has been already found, base class table will not be searched.

If we invoke the method with a reference to the base class then behavior changes. When overriding, as the method address overwrites base class entry, we will call the child method, even when holding a reference to the base class. With shadowing, the base class method table (which is the only one visible as we hold a reference to the base class) contains the virtual method address, and therefore, the base class method will be called.

In general shadowing is a bad idea, as it introduces a difference on the behavior of an instance depending on the reference we have to it.

查看更多
SAY GOODBYE
6楼-- · 2019-01-03 06:49

Hope this brief explanation helps.

Shadowing - Replaces the complete element of the parent class

class InventoryAndSales
{
    public int InvoiceNumber { get; set; }
}

//if someone calls for this class then the InvoiceNumber type is now object 
class NewInventoryAndSales : InventoryAndSales
{
    public new object InvoiceNumber { get; set; }
}



Overriding - Only replaces the implementation. It doesn't replace the data type it doesn't replace like for example you have a variable it doesn't convert it into a method so if there is a method it will use that method and only changed the implementation

 class InventoryAndSales
    {
        public virtual int GetTotalSales(int a, int b)
        {
            return a + b;
        }
    }


    class NewInventoryAndSales : InventoryAndSales
    {
        //it replaces the implementation in parent class
        public override int GetTotalSales(int a, int b)
        {
            return a * b;
        }
    }
查看更多
登录 后发表回答