Inherit a class sealed in C# – C# Programming Example
C# General

Inherit a class sealed in C#

It’s a bit of a trap, of course, a class “sealed” we can not inherit … Yet the need exists. For example a differentiated String. How to get around the ban of the Framework? Is it possible?

Why

This is the first question, and perhaps the most important one. Why want to create descendant types of sealed classes? How can this be useful?

If I speak of utility it is good because the code must answer this imperative, all code without exception. We code to do something useful. Otherwise coding does not make sense.

Take the String class. The framework does not allow the creation of classes by inheriting and to block any desire in this sense, the class String is sealed. The designers of the Framework have definitely closed this door. But they opened another: class extensions. This allows to expand the possibilities of any class, even sealed, so of string too.

This would be perfect if the need to inherit a class was limited to want to add methods … But this is not the only motivation possible!

Take a case in point: you create software that allows you to enter many different class parameters using a PropertyGrid (like the Windows Forms that can be used without problems in WPF). Within such a mechanism you can usually define your own custom editors, which depend on the type of the value. For example, for a property of type Color you will be able to write a publisher offering a Pantone color chart and a “pipette”. This will be more enjoyable for your users than blindly typing a hexadecimal code to define a color.

Imagine for a second that among these parameters that will be entered in a PropertyGrid (or its equivalent WPF or UWP) there are some character strings defining for example the name of an external file.

In such a case you want that in addition to a simple string editor also displays a small ellipsis button “…” that will allow the user to browse the disks to directly select an existing file name. Maybe even the area will handle drag’n drop from the explorer.

Alas … Either you save the new editor for the name of a specific property (which is very constraining and source of bugs), or you save it for its type, String, and then it will be all the strings that will benefit from the file browser, which makes no sense!

What would not it be easier to just define “public class Filename: string {}” and Hop! the case would be played!

The editor would be registered for the type “Filename”, the file names in the parameters would not be of type “string” but of type “Filename” and everything would be for the best in the best of worlds.

So here is a concrete case that shows the obvious utility of creating classes inheriting from string (or other sealed classes), even totally empty, just to create a CLASSification, at the very core of object programming anyway …

I do not doubt that enlighten by this example you find others, even totally different.

In any case we have answered the first question. This is useful, and then object programming is based on inheritance to solve many problems, so there is a natural legitimacy to want to inherit a class. “Sealed” is a little frustrating. It’s almost a misstep in an object world. The justification for the more efficient code produced by a sealed class seems to me to be rather artificial and difficult to defend. But C# is well done, perfection does not exist. Fortunately the great flexibility of the language makes it possible to work around this kind of problem quite easily!

How

I already told you: it is not possible, do not insist! …

But as this ticket would not exist if I did not have a solution to offer you, you say that there must be a “trick”.

The string class is sealed. So there is no magic “thing”. No way to tinker with the Framework either. I told you that it is not possible!

But there is a solution, a little less direct but quite reasonable and “clean”.

It simply means developing another class that does not inherit anything.

Hou there! Reinventing the string type just for a classification reason seems downright overkilling!

That’s right, and we will not be embarking on such a complex path. On the other hand we can be cunning and try to write as little as possible while passing through a string …

In fact it’s quite easy but it uses little used syntactic elements like the implicit operators.

The trick is to create a “normal” class inheriting nothing, and having a single property, Value, of type string (or another type sealed which one would like to inherit).

Of course it’s not hard to write but it does not solve the problem. It is not possible to pass our class for string. Everywhere it will change ‘x =’ foo ” by ‘x.Value = “foo”‘ and that’s not what we’re looking for!

This is to forget the “implicit” operators that make it possible to convert an instance of a class into other types (and vice versa). Implicitly. That is without having to write anything in the code that uses the said class to convert.

To begin we will have a code that looks like this:

The MyString type declares a Value property of type string, but above all, it declares two implicit operators: one that converts one string into MyString, and the other deals with the opposite direction.

It’s almost everything. It works. I can write ‘MyString x =’ foo ” and vice versa (assign a variable of type string directly a MyString type variable).

In reality, it will be necessary to deal with other details, such as equality operators for example, or conversions of type (IConvertible interface), etc.

But most of this code can be directly vampirized from the string class because the Value value is of this type and our class contains nothing else to convert.

We arrive at a final code of this type (assuming as in the above statement that we want to have a custom string to enter the name of a dictionary, so a DictionaryNameString):

And here is a custom “string” class, usable as a string and offering the same services overall in 99% of cases (assignments in one direction or the other, conversions).

Little more: our class is not “sealed” … Just call it “MyStringBase” and then inherit this class to create heaps of custom “string” types.

Apart from the example I gave, we can imagine many cases where doing an “if (variable is MySpecialString) …” can simplify things a lot. While maintaining a simple and clear writing, a clean and maintainable code.

Conclusion

C# is a very subtle language, it knows how to create blockages that avoid large pellets (like the fact that it does not allow multiple inheritance or the existence of sealed classes) but in return it offers many exit doors allowing elegantly to do what you want. It remains to be mastered to get there, and that’s a challenge, the more we work with this language, the more we understand that we are far from knowing everything!

 

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: