views:

1190

answers:

4

In C#, is it possible to extend a class that has no constructors?

Maybe I'm thinking about this incorrectly and just need a kick in the crotch. I have a Silverlight class that extends System.Windows.Media.Transform, With the official release of Silverlight 2, Transform now has no constructor. So, when I compile my class, I get an error saying that 'The type '...Transform' has no constructors defined.'

Is it still possible to extend this class in a useful way? If not, I'm going to be drawing an awful lot of sad faces.

+7  A: 

The sole constructor to Transform is internal, so you can't derive from it yourself.

Jon Skeet
:( See that? That's a start.
MojoFilter
WTF? Why didn't they just seal it, crippling it like they did for Graphics and other related classes?
plinth
Because they needed to derive from it within their own assembly. This isn't an uncommon pattern.
Jon Skeet
Yeah, there's really nothing wrong with it. I guess I've learned my lesson about developing against stuff in Beta.
MojoFilter
A: 

Hiding all public constructors is a technique used to prevent subclassing and force developers to use the class as intended. It may be the implementor wants you to use an Adapter or Facade or even a Proxy at the time of extension.

There may be important lifecycle details in the constructor that require the use as the original implementor intended, or it may be an oversight by the developer.

I've seen attempts at conversion to the Factory pattern where this is done in a manner that prevents subclassing. I.e. there is no protected constructor made available to subclasses. Sometimes advanced object patterns can strain the in-built capabilities of a language.

caskey
A: 

It is impossible that some class has no constructor. Every class has at least one AFAIK. If you don't write it C# compiler will insert an default (parameterless) constructor for you.

Petar Repac
But the default constructor does not need to be public. If it's private, nobody except the class itself can access it, and if it's internal, at least no external code can access it.
OregonGhost
It's not impossible to write a class with no constructor, as of C# 2: static classes have no constructors.
Jon Skeet
(And besides, it's always been possible in IL.)
Jon Skeet
A: 

As long as its not sealed, you should be able to extend it.

Not true, even within the same assembly - if it only has private constructors (and you can't embed the new type as a nested type) you're screwed. Basically the C# compiler forces you to call a base constructor, so you've got to be able to access one...
Jon Skeet
You're right. If the class explicitly defines a private constructor then teh compiler won't generate a default constructor. But I was considering "no constructor" - meaning the creater of the class didn't write a construtor. Which would mean the the compiler will generate a default constructor.
Not if it's a static class - they have genuinely no constructors :)
Jon Skeet
Oh, snap! True dat!
MojoFilter