I guess with what I’m going to write I should first clarify, it’s not something I would like to achieve. It is something that happened to me and I am trying understand how it is possible, so I can fix it. So here we go…
I’m using C#, .Net 4.0. The code is biggish, too big to past it all here but I try to explain what happens in hope that there is somebody knowledgeable who will have some thoughts.
In my call stack I have a series of generic methods and I have noticed that although the value should be just passed from one to the other it is changing. Ok, that was my first impression. Later I managed to isolate one problematic method in which when I stop I can see two different values for the same property of an object.
public class Sample : BaseClass, ISomeInterface
{
[XmlIgnore]
public new Guid Id { get; set; }
}
What may be significant that both BaseClass and ISomeINterface define
public Guid Id
so now, when I stop in that generic method and watch variable data of Sample type i can expand its properties and see the first value of Id. But when I watch data.Id it shows different value. Have a look for yourself.
(Here was a picture which I cannot post due to a negligible reputation. Sorry)
Edit: I pushed it there http://picturepush.com/public/7307446
Would any body out there know what is the difference in how those values in the Watch window are obtained? What is the difference? I tried many different approaches, casting, using reflection but always I get the value the same as when you watch data.Id and ironically, the correct value, the one I expect is the other, elusive one.
Oh, and no, it is not a homework 😉
The
newkeyword is hiding the base class member.Set the
Idproperty inBaseClassto virtual:And override it in
Sample: