I have a class with two methods, one of which overrides a base class method:
public override void Write (byte[] b)
public void Write<T> (IList<T> b)
Then make the call:
byte[] bArray = new byte[] {1,2,3,4};
myClass.Write (bArray);
... but it is the Write (IList<T>) which gets called, even though Write (byte[]) is an exact match. This seems very wrong!
Full compilable example below:
using System;
using System.IO;
using System.Collections.Generic;
public class TestClass : BinaryWriter {
public override void Write (byte[] b) {
Console.WriteLine ("Write(byte[]) called");
}
public void Write<T> (IList<T> b) {
Console.WriteLine ("Write(IList<T>) called");
}
}
public class MainProgram {
static void Main (string[] args) {
TestClass tc = new TestClass();
byte[] bArray = new byte[] {1,2,3,4};
tc.Write (bArray);
}
}
Is this a framework bug

Wrong overload being called
kalai
Can-Ann
I'm going to go and suggest that in the FXCop forum.
Daniel Danilin
Patrick Travers
RabinLin
Visual Studio and .NET Framework Feedback
Bjoern.Greiff
There's currently a bit of debate about what's going on in this case. It appears (either erroneously or not) override-resolution rules are making your non-"override" methods take precedence, and since byte[] implements IList, your Write<T>(IList<T>) is being called.
A workaround would be not to override the Write method with something that could be used with a byte[] parameter (in your case IList<T>; but IList, ICollection, and possibly ICloneable would cause the same problem).
I'll add posts to this thread with updated information, as I receive it.
TheQuietShadow
Well, in that case it's a candidate for a compiler warning.
PamelaO
http://connect.microsoft.com/feedback/default.aspx SiteID=210
(You have to register there to report bugs tho')
marco.ragogna
This is not a bug. It's how the C# language is supposed to handle overrides, based upon the current specification.
Method invocation resolution works in several passes. The first pass looks at only applicable, non-overridden, accessible methods in the type indicated by the method invocation. Since, in your example, Write<T>(IList<T>) is the only method that fits that criteria, it is chosen as the method to call.
[edit: this is covered by Member lookup in the the spec.; section 14.3]