Extending existing classes

One other point that came up in the static import thread was extending existing classes. It's not uncommon to want to add specific methods to existing classes - or at least have the appearance of doing this. For example, I might want to add a new method to the string class, so I can call it with:

string s = ...;

string r = s.Permute(10, 15);

rather than

string r = Utils.Permute(s, 10, 15);

We've discussed this a few times in the past, and we think this is an important scenario to consider. There are a number of reasons why you wouldn't want to actually modify the class, of which security is just one consideration. But one could think of a way of writing something like (very hypothetical syntax):

class Utils
{
    [AddTo(typeof(string))]
    public static string Permute(string s, int a, int b) {...}
}

and have the compiler then allow you to use it as if it were part of the string class. This is very useful, but perhaps not terribly understandable, and would certainly be open to abuse.

Another option would be to allow the following definition (also hypothetical)

class MyString<T>: T where T:string
{
    public string Permute(int a, int b) {...}
}

Now, if you use a MyString, you can add methods onto the existing method. This would also be useful to add a specific implementation of something onto an existing class, somewhat in the way that Mixins work.

We have no plans in this area, but will likely discuss the scenario more in the future.