KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I had thought that Generics in C# were implemented such that a new class/method/what-have-you was generated, either at run-time or compile-time, when a new generic type was used, similar to C++ templates (which I've never actually looked into and I very well could be wrong, about which I'd gladly accept correction). But in my coding I came up with an exact counterexample: static class Program { static void Main() { Test testVar = new Test(); GenericTest<Test> genericTest = new GenericTest<Test>(); int gen = genericTest.Get(testVar); RegularTest regTest = new RegularTest(); int reg = regTest.Get(testVar); if (gen == ((object)testVar).GetHashCode()) { Console.WriteLine("Got Object's hashcode from GenericTest!"); } if (reg == testVar.GetHashCode()) { Console.WriteLine("Got Test's hashcode from RegularTest!"); } } class Test { public new int GetHashCode() { return 0; } } class GenericTest<T> { public int Get(T obj) { return obj.GetHashCode(); } } class RegularTest { public int Get(Test obj) { return obj.GetHashCode(); } } } Both of those console lines print. I know that the actual reason this happens is that the virtual call to Object.GetHashCode() doesn't resolve to Test.GetHashCode() because the method in Test is marked as new rather than override. Therefore, I know if I used "override" rather than "new" on Test.GetHashCode() then the return of 0 would polymorphically override the method GetHashCode in object and this wouldn't be true, but according to my (previous) understanding of C# generics it wouldn't have mattered because every instance of T would have been replaced with Test, and thus the method call would have statically
Tags (comma-separated)
Save Edits
Cancel