Jaja1415
Jaja1415

Reputation: 113

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?

Upvotes: 0

Views: 1101

Answers (2)

Ipsit Gaur
Ipsit Gaur

Reputation: 2927

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
    }
}

Upvotes: 1

Prateek Shrivastava
Prateek Shrivastava

Reputation: 1937

Example of Extension Method:

public static class WorkItemExtensions
    {
        public static void ReadWrite(this WorkItem objRef)
        {
            // Do things you need here
        }
    }

Upvotes: 2

Related Questions