[Mono-list] I do not know if C# generic has a bug here

Stifu stifu at free.fr
Tue Nov 29 13:50:07 EST 2011


Not a bug. The result should be B.G(). It is the same with .NET.
If you want D.G(), mark your method as virtual, and override it. Like this:

class B {
     public virtual String G() { return "B.G()"; } 
}  

class D : B {
     public override String G() { return "D.G()"; } 
}

It's different in Java because everything is virtual by default.


cpwang wrote
> 
> For the following code:
> 
> class B {
>      public String G() { return "B.G()"; } 
> }  
> 
> class D : B {
>      public String G() { return "D.G()"; } 
> }  
> 
> class TestCompile {
>      private static String TestG<T>(T b) where T: B {return b.G(); }
>      static void Main(string[] args) { TestG(new D()); } 
> } 
> 
> The result is B.G(), whereas the result of similar C++ or java code would
> be D.G().
> 


--
View this message in context: http://mono.1490590.n4.nabble.com/I-do-not-know-if-C-generic-has-a-bug-here-tp4119905p4120301.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list