Friday, October 2, 2009

Extension methods

It was not that difficult to create extension after all.

At one moment I found out that String type do not allow me to surround a string with double quotas.
So I wrote the following extension to String type:
public static class Extensions
{
    public static String SurroundWith(this String str, Char chr)


    {
        return chr + str + chr;
    }
}

This extension I can then use in my program:
String fileName =
@"C:\Program Files (x86)\Microsoft Office\OFFICE11\Excel.exe";
fileName = fileName.SurroundWith('"');

fileName = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.csv");
Process.Start(new ProcessStartInfo("Excel.exe", fileName.SurroundWith('"')));

No comments:

Post a Comment