How to extend methods to a class not to its instances.

Posted by Fraga on Stack Overflow See other posts from Stack Overflow or by Fraga
Published on 2010-05-02T01:14:38Z Indexed on 2010/05/02 1:17 UTC
Read the original article Hit count: 488

Hi.

Extending methods to any instance is really easy:

public static string LeaveJustNumbers(this string text)
{
    return Regex.Replace(text, @"[\D]", "");
}
...
string JustNumbers = "A5gfb343j4".LeaveJustNumber();

But what if i want to extend methods to a sealed class like string, to work like:

string.Format("Hi:{0}","Fraga");

Is there any way to do it?

© Stack Overflow or respective owner

Related posts about c#

Related posts about extending