Property value "get" returns 0 even though there's a value
See original GitHub issueThis happens on the WASM in a blazor project. When in debug and hovering on c.Id you can see there is a value, but when you assign it to a variable it assigns the value 0. This causes the ToDictionary function to fail because of duplicate keys (0) while there is no Id with a value of 0 in the collection.
When I am in debug mode and I hover over c.Id, it shows the id, when I assign it to a variable and I hover over that variable it shows 0. How is that even possible?
Here’s the simplified version of the code that fails
private void CollectionToDictionary<T>(List<T> collection) where T : IUniqueResource { var dictionary = collection.ToDictionary(t => t.Id, t => t); return dictionary; }
Here’s the simplified code of the class implementing : IUniqueResource ` public interface IUniqueResource { public long Id { get; } }
public class BaseDealerLocation : IUniqueResource
{
public long Id { get; set; }
}
public class DealerLocation : BaseDealerLocation
{}
public class GameTable : DealerLocation
{}
`
Here’s a simplified sample of what the data being passed to the method looks like:
var gameTables = new List<GameTable> { new GameTable { Id = 1 }, new GameTable { Id = 2 }, new GameTable { Id = 3 }, new GameTable { Id = 4 }, new GameTable { Id = 5 }, };
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (7 by maintainers)
That might be a bug in the debugger/inmediate window where it’s showing the field on the base class instead of the value the runtime produces (that’s why the differences happen, but TBH this is a C# question, so don’t take me as an authoritative source 😃)
This has been historically the C# behavior. WarningsAsErrors is always an option here. In general it’s discouraged to hide members unless you truly know what you are doing.
That’s not how hiding works. Your property is not virtual, and the binding to
get_Id()
is done at compile time based on the compile time type. Your generic method is calling IUniqueResource.Id which is implemented by BaseTypeThanks @danypellerin. We’ll wait for you to share a small repro project so that we can move forward with this.