System.Drawing.Graphics.DrawImage(Image image, int x, int y) WTF???

I just saw this over on thedailwtf.com:

public void DrawImage(Image image, int x, int y);

Declaring Type: System.Drawing.Graphics
Assembly: System.Drawing, Version=1.0.5000.0

public void DrawImage(Image image, int x, int y)
{
if (this == null)
{
throw new ArgumentNullException("this");
}

if (image == null)
{
throw new ArgumentNullException("image");
}
int num1 = SafeNativeMethods.GdipDrawImageI(new HandleRef(this, this.nativeGraphics), new HandleRef(image, image.nativeImage), x, y);
this.CheckErrorStatus(num1);
}


I verified this with Lutz Roder's reflector. I see that those programmers over at MS are mortals just like the rest of us. :-)

4 Comments

  • When System.Drawing was written, C# allowed null this pointers... It was later that ECMA disallowed C# from allowing instance methods to be called on null pointers.

  • How could you have even called an instance method with a null this pointer? I'm assuming this must have been a managed<->unmanaged code scenario.

  • I'm guessing here, but would invoking a method through reflection and passing null as the instance cause this check to be necessary, at least as long as there's no ECMA-compliancy test that disallows this even earlier.

  • Prior to VS.NET Beta 2, you could call



    Graphics g = null;

    g.DrawImage(...);



    In fact, the ability to call non-virtual methods on a null pointer was mentioned as a feature in Eric Gunnerson's first book on C#.



    However, ECMA vetoed this feature when Microsoft submitted it for standardization.

Comments have been disabled for this content.