It redeclares a member that is not overridable. Only calls to the instance will be affected. Code inside the base classes will not be affected by this.
Public Class Person
Public Sub DoSomething()
Console.WriteLine("Person")
End Sub
Public Sub UseMe()
Me.DoSomething()
End Sub
End Class
Public Class Customer
Inherits Person
Public Shadows Sub DoSomething()
Console.WriteLine("Customer")
End Sub
End Class
Example usage:
Dim p As New Person
Dim c As New Customer
p.UseMe()
c.UseMe()
Console.WriteLine("----")
p.DoSomething()
c.DoSomething()
Output:
Person
Person
----
Person
Customer
Pitfalls:
Example1, Creating a new object through a generic. Which function will be used??
Public Sub CreateAndDoSomething(Of T As {Person, New})()
Dim obj As New T
obj.DoSomething()
End Sub
example usage:
Dim p As New Person
p.DoSomething()
Dim s As New Student
s.DoSomething()
Console.WriteLine("----")
CreateAndDoSomething(Of Person)()
CreateAndDoSomething(Of Student)()
Output: By intuition the result should be the same. Yet that is not true.
Person
Student
----
Person
Person
Example 2:
Dim p As Person
Dim s As New Student
p = s
p.DoSomething()
s.DoSomething()
Output: By intuition you could think that p and s are equal and will behave equal. Yet that is not true.
Person
Student
In this simple examples it is easy to learn the strange behaviour of Shadows. But in real-life it brings a lot of surprises. It is advisably to prevent the usage of shadows. One should use other alternatives as much as possible (overrides etc..)