Beginning Extension Methods
Submitted by brian on Sun, 12/14/2008 - 9:56pm.
Extension methods have been around since C# 3.0 came out, which I guess was some time ago. I really like using them. The syntax is short and neat. Notice how the example below lives in a static class. Nutty eh!?
This example is for all you foos out there that might not be checking input before it's passed down into the database. It checks the input string for null/empty and if there's something, it will replace a single quote with two single quotes (since the single quote is arguably the most devastating character in user inputs / SQL inserts)
public static class StringUtils { public static string ToSafeSql(this string s) { return (s == null || s.Trim().Length == 0) ? "" : s.Replace("'", "''"); } }
Tags:
















