Search code examples
c#classframeworksextension-methodssealed

C# - Extending existing framework sealed class


Consider the sealed framework class "WorkItem" (MSDN-description)

I would like to extend this sealed class so I can write extension methods of the class e.g. I could write:

workitem.ReadWrite();

Is this possible to implement, if so how could it be done?


Solution

  • It is very much possible to add extension methods to sealed classes. You can simply create an extension method for a sealed class like a concrete class.

    You can write like this for your class, Also you need to include the assembly and use the namespace of the Extensions class where you will be using the method.

    public static class Extensions 
    {
        public static void ReadWrite(this WorkItem item)
        {
            // Do your code
        }
    }