|
 |
Kyle wrote:
> I haven't read the whole article yet, only up to the first example, but
> am wondering why not just use a new class that inherits the string class
> and add an email check function there? I'll finish the article later...
Say, for instance you don't own the class you're extending and the class
is sealed (you can no longer inherit) the function you want to add
doesn't need the internals of the class, but would be convenient if it
were part of the class. Or say you don't want to have to declare all
strings that use your new method as
KylesStuff.Coolthings.KylesCoolString you'd rather keep your existing
codebase, and have this extension available to the new code that will be
using it. Obviously a function that takes a KylesCoolString isn't going
to accept a string, though you could create an implicit conversion, just
to make things easier.
or, you simmply add an extension function to string, and not deal with
the issues surrounding inheritance. The extension function simply acts
as if it was there the whole time. A way of extending a class without
messing with the class or creating a new class, if you will.
--
~Mike
Post a reply to this message
|
 |