2

I want to derive the class TestImage from the System.Drawing.Image class. However if I try to instantiate TestImage like so:

class TestImage : Image {
    public TestImage() {
    }
}

I get the error CS1729: 'Image' does not contain a constructor that takes 0 arguments. Image is an abstract class though, so it doesn't have or need a constructor, right? Why am I getting the error?

Btw I know that it is propably smarter to derive off System.Drawing.Bitmap, this more of a theoretical question.

Jan Berndt
  • 913
  • 1
  • 10
  • 22
  • 9
    [System.Drawing.Image](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image?view=netframework-4.8) does not have any public constructor. It has constructors with internal access specifier. That's why you are getting this error. – Chetan Aug 18 '19 at 11:36
  • @ChetanRanpariya but how do classes like System.Drawing.Bitmap derive from Image then? – Jan Berndt Aug 18 '19 at 11:44
  • 6
    @CheeseCrustery Because `Bitmap` is in the same assembly, so it has access to the [`internal` constructor](https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Image.cs,e383648bb61b2878,references). – GSerg Aug 18 '19 at 11:57
  • Not sure if it's the case for you, but, coming from Java very easily gives you the habit to subclass inbuilt classes to expand them. However, C# simply does not work that way. It is a difference in programming philosophy. You can make your own class containing and exposing an Image, but you can't make your own class derived from `Image` itself. – Nyerguds Sep 16 '19 at 13:38

0 Answers0