Before you get excited, this post is just about floating an idea rather than explaining how to do something, but bear with me and see if you think it'd be handy.

Imagine you're given a class library with a Customer class in it. You want to make a nice little modal edit form so your user can edit a Customer, and the form should have an OK and Cancel button.

You put together your form, and it looks sweet. So then you start typing the code to bring up the form:

Customer cust = GetCustomer();
CustomerForm custForm = new CustomerForm(cust);
cust.BeginEdit();
if (custForm.ShowDialog() == DialogResult.OK)
{
cust.EndEdit();
}
else
{
cust.CancelEdit();
}

Looks great, huh? Except ... you compile, and you realise that the creator of the Customer class never bothered to implement the IEditableObject interface.

So now you have to write your own little helper methods to save off the properties of your Customer, and put them back when the user clicks Cancel. Ugh.

Wouldn't it be nice if you could "impose" an interface onto a class, and tell the compiler what to call when the interface methods/properties are called? So in the example above you could define the IEditableObject methods (BeginEdit, EndEdit and CancelEdit) for Customer, and tell the compiler that Customer does indeed implement IEditableObject - and to use those methods for its implementation.

I have no idea what the syntax might look like. Some take on the "this" keyword you use to define an extension method, I suppose. Perhaps something like this:

public static class CustomerStuff
{
public static void IEditableObject.BeginEdit(this Customer cust)
{
// save away the customer
}


public static void IEditableObject.CancelEdit(this Customer cust)
{
// roll the customer back
}
}

The compiler, of course, would have to complain if you didn't implement the entire interface (notice I've forgotten EndEdit in the example above).

Maybe my IEditableObject example is a bit weak, but imagine if you wanted to define your own implementation of IComparable<T> for a class that you didn't own. Or ISerializable! Would that be worth something to you?